Operating System - HP-UX
1827279 Members
3374 Online
109717 Solutions
New Discussion

how replace replace a particular string repeated for many times

 
SOLVED
Go to solution
jayachandran.g
Regular Advisor

how replace replace a particular string repeated for many times

Hi All

i have a file which contains a word "value" for repeated no. of times and i want to replace that word to "rate" how do i do.

replacing mainually is not that much possible.

Help me please

12 REPLIES 12
john korterman
Honored Contributor
Solution

Re: how replace replace a particular string repeated for many times

Hi,
try
# sed 'value/rate/g' your_file

regards,
John K.
it would be nice if you always got a second chance
Ranjith_5
Honored Contributor

Re: how replace replace a particular string repeated for many times

Hi,

# vi file

the do the command

:%s/value/rate/g

this will do ur work.


Regards,
Syam
jayachandran.g
Regular Advisor

Re: how replace replace a particular string repeated for many times

What to do if it is multiple files in same directory
Franky_1
Respected Contributor

Re: how replace replace a particular string repeated for many times

Hi,

you could also try

vi

:%s///g

That's it

Franky
Don't worry be happy
Muthukumar_5
Honored Contributor

Re: how replace replace a particular string repeated for many times

In vi editor,

:%s/value/rate/gi

g - globally changes it
i - user input to do if yes no if no

Using perl,

perl -pi -e 's/value/rate/g'

Using sed,

sed -e 's/value/rate/g' > newfilename
mv newfilename filename

hth.
Easy to suggest when don't know about the problem!
Franky_1
Respected Contributor

Re: how replace replace a particular string repeated for many times

Hi again,

for i in `ls *`
do
perl -pi -e 's///g' $i
done

HTH

Franky
Don't worry be happy
Vibhor Kumar Agarwal
Esteemed Contributor

Re: how replace replace a particular string repeated for many times

One with sed:

for i in `ls *`
do
sed -e 's///g' $i
done
Vibhor Kumar Agarwal
H.Merijn Brand (procura
Honored Contributor

Re: how replace replace a particular string repeated for many times

1. perl -pi does not have to placed in a loop. It'll safely do so for all files on the command line

2. More important. Nobody protected the "value" against being part of another word, like "valued" or "invalueable" (give or take the typoes). The pattern to be replaced, if needed to be recognized as a word, needs to be anchored!

# perl -pi -e's/\bvalue\b/rate/g' file1 file2 file3 ...

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Muthukumar_5
Honored Contributor

Re: how replace replace a particular string repeated for many times

For multiple files in the same directory then,

perl -pi -e 's/value/rate/g' /*

Multiple files in multiple directory then,

find / -type f -name "*" -exec perl -pi -e 's/value/rate/g' {} \+

You can change * in change depends upon file pattern like *.log

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: how replace replace a particular string repeated for many times

You can also use xargs + find + perl -pi do change pattern in multiple files in different directories as,

# find -type f -name "*" | xargs perl -pi 's/value/rate/g'

hth.
Easy to suggest when don't know about the problem!
Sandman!
Honored Contributor

Re: how replace replace a particular string repeated for many times

Yet another way of replacing the desired string on multiple files within a directory. ex works on the existing file so that renaming or moving the original file is not required. Also this replaces whole words only instead of substrings like "valued".

# for i in $(ls ); do
> cat - <> %s/\/rate/g
> wq
> EOF
> done

regards!!
jayachandran.g
Regular Advisor

Re: how replace replace a particular string repeated for many times

Hi all
Thanks for all your valuble responce.
thank you.