1842567 Members
2810 Online
110189 Solutions
New Discussion

Find and Replace command

 
SOLVED
Go to solution
Mridul Dutta
Advisor

Find and Replace command

Hi Friends,

i have created a file name like myfile.txt , Now i want to search a word in that file and replace with a new word without redirecting to another file.
N:B: i can do this in vi editor . but i want this from outside ( using the file itself)

Mridul
6 REPLIES 6
Senthil Prabu.S_1
Trusted Contributor
Solution

Re: Find and Replace command

Hi,

One possible way is to using sed, then writing into another file, then moving back to original file.

# cat 1
senthil
# sed s/senthil/prabu/g 1 > 2; mv 2 1;
# cat 1
prabu

P.S:
Not the best solution

HTH,
Prabu.S
One man's "magic" is another man's engineering. "Supernatural" is a null word.
Rasheed Tamton
Honored Contributor

Re: Find and Replace command

Hi Mridul,

Without re-directing to another file - I have not noticed yet.

We normally use sed as below:

sed 's/oldtext/newtext/g' filename

But of course, you need to redirect it to a file if you want to save it - there is no online saving on sed as far as I know.

ed is an option (by automating the steps).

Rasheed Tamton.
Hein van den Heuvel
Honored Contributor

Re: Find and Replace command

cat > xx
aap
noot
mies
$ perl -pi -e 's/noot/vuur/' xx
$ cat xx
aap
vuur
mies

Oviwan
Honored Contributor

Re: Find and Replace command

Hey

you can use perl:

#perl -i.del -ple "s/find/replace/" filename

generates an old file named filename.del. after you replaced every file you can delete all *.del files.

Hope this helps

Regards
Sandman!
Honored Contributor

Re: Find and Replace command

How about the following ex(1) construct:

# ex -s +'%s/search_word/replace_word/g | wq' infile

~cheers
Arturo Galbiati
Esteemed Contributor

Re: Find and Replace command

Hi Mridul,
by vi is possibile to change by command line:

vi -c "%s/search_word/replace_word/g|wq" file.in

HTH,
Art