1836603 Members
3177 Online
110102 Solutions
New Discussion

replace word in a file

 
ust3
Regular Advisor

replace word in a file

there are 300 files in a directory , some of these files has a word "error" , I word to change this word to "message" , can advise what can i do ? thx.
14 REPLIES 14
Shrikant Lavhate
Esteemed Contributor

Re: replace word in a file

Hi ust3,

For single file use

cat | sed '/error/s/error/message/g' >

This will replace all error word into message.

For all files into folder you can run a for loop with this command inside.

-=ShRi=-
Will it remain a personal, if I broadcast it here!
Pete Randall
Outstanding Contributor

Re: replace word in a file

Use sed:

sed 's/error/message/g'


Pete

Pete
Yogeeraj_1
Honored Contributor

Re: replace word in a file

hi,

Create a script named process.sh as follows:
#!/bin/sh
until [ -z "$1" ]; do
if [ -f $1 ]; then
cat $1 | sed s/error/message/g> /tmp/newfile
cp /tmp/newfile $1
fi
#
shift
done

then run the script as follows:
./process.sh


hope this helps!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Yogeeraj_1
Honored Contributor

Re: replace word in a file

hi again,

below an example that i used last time:

Server1: home/yogeeraj> ALLFILES=$(ls -1 /home/yogeeraj/mydir/*.htm)
Server1: home/yogeeraj> ./process.sh $ALLFILES


hope this helps too!
kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Franky_1
Respected Contributor

Re: replace word in a file

Hi ust3,

if you have several files in one dir the following script will rename them :

for i in `ls `
do
k=`echo $i|sed 's//error/g'`
mv $i $k
done

Regards

Franky
Don't worry be happy
James R. Ferguson
Acclaimed Contributor

Re: replace word in a file

Hi:

You need to be more explicit at several levels. I'll assume that you want to recursively find every *file* in a particular directory and that you want to replace the word "error" (in either upper or lower case?) whenever it appears on a line:

# cd /path && find . -xdev -type f -exec perl -pi.old -e 's/\berror\b/message/ig' {} \+

THis will perform the operation in your current directory; not cross mountpoints; find only files; and in-place replace every occurance of "error" with "message" _as_long_as_ the string "error has a boundry around it (whitespace, end-of-line, beginning of line, etc.). The update will be done to the file, in place, but a backup copy will be left as the name of the file suffixed with ".old".

Regards!

...JRF...
ust3
Regular Advisor

Re: replace word in a file

thx all replies ,

I think below script is good , but I can't run it , can advise how should I modify it to suit my requirement ? thx


find . -xdev -type f -exec perl -pi.old -e 's/\berror\b/message/ig' {} \+
James R. Ferguson
Acclaimed Contributor

Re: replace word in a file

Hi (again):

If perchance your release level is 11.0 or earlier, substitute a semicolon for the "+" character at the end:

# find . -xdev -type f -exec perl -pi.old -e 's/\berror\b/message/ig' {} \;

If that doesn't work, post the error you receive.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: replace word in a file

>ShRi: cat | sed '/error/s/error/message/g' >

sed(1) takes a filename, no need to use cat(1). AND reading the file at the same time you are overwriting it will get you into trouble. Using /error/ in both the pattern and the substitution is overkill.

>yogeeraj: below an example that i used last time:

You can combine the two:
$ ./process.sh /home/yogeeraj/mydir/*.htm

>Franky: if you have several files in one dir the following script will rename them

ust3 wanted to change the contents not the name.

>Franky: k=`echo $i|sed 's//error/g'`

There are problems here. You likely are changing all files to the same name. This will make all but the last go poof. You probably want to put $i in for old_file_name but then you need to use "". And you aren't likely to need the "g" at the end.

>JRF: If perchance your release level is 11.0 or earlier, substitute a semicolon for the "+" character at the end:

-exec ... + is supported on 11.00:
http://docs.hp.com/en/B2355-90680/find.1.html
ust3
Regular Advisor

Re: replace word in a file

thx replies,

find . -xdev -type f -exec perl -pi.old -e 's/\berror\b/message/ig' {} \;

I think it will be work in unix , but if I want to use it in linux , can advise how can I change the script ? thx
James R. Ferguson
Acclaimed Contributor

Re: replace word in a file

Hi (again):

> I think it will be work in unix , but if I want to use it in linux , can advise how can I change the script ?

There is nothing to change unless you need to add an absolute path to either the 'find' or the 'perl' executable. The syntax should work equally well in GNU/Linux.

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor

Re: replace word in a file

Hi :

By the way, once you find that you are satisfied with the answers that you have received, please give consideration to this:

https://forums1.itrc.hp.com/service/forums/helptips.do?#28

Regards!

...JRF...
ust3
Regular Advisor

Re: replace word in a file

thx reply ,

back to the below script ,

for i in `ls `
do
k=`echo $i|sed 's//error/g'`
mv $i $k
done

when I run it , the error is

mv: `file1' and `file2' are the same file

can advise how can I change it ? thx
Dennis Handly
Acclaimed Contributor

Re: replace word in a file

>back to the below script

Did you see what I said about the script?
This will make all but the last go poof.

>can advise how can I change it?

Why would you want to use this script after I mentioned all it its problems? If you want to RENAME mass quantities of files, it would still need some changes.