Operating System - HP-UX
1833857 Members
2330 Online
110063 Solutions
New Discussion

Re: Word substitution using SED

 
praveenkumar.K
Occasional Contributor

Word substitution using SED

hi all,
i want to replace a word in a file,and rename the file(file with replaced word)back to the original name.I don't know the filename but i know the type of file,i.e .txt etc..and there are mulitple files of the same type..eg:- a.txt,b.txt etc..the script should open all the .txt files and replace the word if found and the files should have their original names respectively...

TIA
Praveen.K
8 REPLIES 8
Frederic Sevestre
Honored Contributor

Re: Word substitution using SED

Hello,

Try this :

for FILE in $(ls | grep ".txt")
do
sed -e "s/word/newword/" $FILE > $FILE.new
mv $FILE $FILE.save
mv $FILE.new $FILE
done

It should work.

Fr??d??ric


Crime doesn't pay...does that mean that my job is a crime ?
Arturo Perez del Galleg
Frequent Advisor

Re: Word substitution using SED

Hi Praveen, see this scripts, do you must to replace word1 and word2 with your words. Is this that you want?

#!/usr/bin/sh
for file in *.txt
do
sed 's/word1/word2/g' $file >$file.$$
mv -f $file.$$ $file
done

HTH
Tom Maloy
Respected Contributor

Re: Word substitution using SED

Or you can use perl and edit in-place:

for i in *.txt
do
perl -i -e "s/word1/word2" $i
done
Carpe diem!
Chia-Wei
Advisor

Re: Word substitution using SED

for i in $(ls "*.txt")
do
sed -e 's/word/replace/g' $i > $i.$$
mv $i.$$ $i
done
KapilRaj
Honored Contributor

Re: Word substitution using SED

Praveen,

You can achieve the output you are looking at form the above responses that you have. I would like to add some points while writing a shell scripts.

1. Be sure that the temporary files that are created by the scripts are removed.
2. Be sure you have kept a backed up the original files which you intend to modify in the script
3. i do not remember more . So if i am given a request like this ... i would like to write a script like this

**************************************
#!/usr/bin/ksh
#Script to search & replace
for i in `ls *.txt` #get a list of files
do
#Replasing string "word1" in each text file with string "word2"
cat $i |sed -e "s/word1/word2/g" >$i.new
mv $i $i.old.`date` #Backup of original files
mv $i.new $i
done
***************************************

Cheers .............

kaps
Nothing is impossible
Wim Rombauts
Honored Contributor

Re: Word substitution using SED

You didn't say all the files you need to change are in 1 and only 1 directory.

If you need to scan a directory including it's subdirectories, you could use

find -type f -name "*.txt" -exec <script_name> {} \;

And then create a script with the "sed" or "perl" and "mv" commands from the above examples :

file=$1
sed -e '/oldword/newword/g' $file > $file.new
mv $file.new $file
exit

You don't need the for - do - done loop anymore, because "find" will run the script for every single file found.

Good luck.
Anthony deRito
Respected Contributor

Re: Word substitution using SED

Do it easily with awk....

cat $ORIG_FILE | awk -v word1="$var_one" -v word2="$var_two" -v word3="$var_three" -v word4="$var_four" '
{
if ($1 == "replace_word")
{
sub(word1,word2);
sub(word3,word4);
}
print
} ' > $NEW_FILE


Hope this helps...

Tony
Paul Sperry
Honored Contributor

Re: Word substitution using SED

try something like this:

#!/usr/bin/ksh
find /DIR -type f | while read FNAME
do
sed "s/OLDSTRING/NEWSTRING/g" < ${FNAME} > ${FNAME}.tmp
mv ${FNAME}.tmp $FNAME
done