On Mon, 2003-01-13 at 06:06, Rechenberg, Andrew wrote: > I don't believe that you can't substitute with grep or find. Do you > have access to sed? > > sed -e 's/abdfgg/opsmsdd/g' filename > outputfilename > > If you have multiple files with text you wish to replace, then you could > use 'grep -R' to find them and if you're replacing with the same text in > each file, then you could use the sed command in a loop and replace > everything. > > sed is your friend.
It's more of an acquaintance <wink>, nevertheless, searching and
replacing text is a common activity, so here's a short script to search
a directory tree for files containing a string and replace occurrences
of that string using find, grep, and sed:
<warning>
If you can't decipher this, be aware that this searches the *entire*
current directory (subdirectories included), and replaces *every*
occurrence of given string in *every* file.
</warning>
#!/usr/bin/env bash
function help() {
echo "Searches entire current directory *tree* and replaces"
echo "all occurences of <string1> with <string2> in all files."
echo "Original file saved to <filename>.original."
echo
echo "Usage: `basename $0` <string1> <string2> [.ext]"
echo
echo "Examples: "
echo " `basename $0` foo bar .txt"
echo " would replace the string foo with the string bar in
*.txt"
echo " `basename $0` foo bar"
echo " would replace the string foo with the string bar in ALL
files"
echo
exit
}
if [ $# -lt 2 ]; then
help
fi
for f in `find . -type f -name "*$3" | xargs grep -l $1`; do
sed "s/$1/$2/g" $f > $f.new
mv $f $f.original
mv $f.new $f
done
>
> Hope this helps,
> Andy.
>
>
> -----Original Message-----
> From: Jianping Zhu [mailto:[EMAIL PROTECTED]]
> Sent: Monday, January 13, 2003 8:22 AM
> To: [EMAIL PROTECTED]
> Subject: how to substutue string in a text file
>
>
>
> how to substutue string in a text file by using "gerp" or "find"?
>
> I have a text file, lof of "abdfggg" in that text file, i need to change
> it to "opsmsdd", is there a simple way to do that?
>
> Thanks
>
>
> --------------------------------
> Jianping Zhu
> Department of Computer Science
> Univerity of Georgia
> Athens, GA 30602
> Tel 706 5423900
> --------------------------------
>
>
>
> --
> redhat-list mailing list
> unsubscribe mailto:[EMAIL PROTECTED]?subject=unsubscribe
> https://listman.redhat.com/mailman/listinfo/redhat-list
--
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308 (800) 735-0555 x308
signature.asc
Description: This is a digitally signed message part
