Solved

replace string in all the files

Posted on 2003-11-10
8
666 Views
Last Modified: 2013-12-27
Hi,

I want to replace a string "abc" with "xyz" in all the files, in current directory and all its subdirectories. How to do that ?
I do not want to open each and every file and replace the string.

Thanx in advance,
Vaibhav
0
Comment
Question by:vaibhavmishra062201
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9714311
Hi vaibhavmishra,

try this

ls -R | while read i
do
     sed -e 's/abc/xyz/g' $i
done

Cheers!
Sunny:o)
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9714328
oops

ls -R <name of directory to which you want to apply changes>| while read i
do
     sed -e 's/abc/xyz/g' $i > $i.new
done

moreover.. this will store modified files in filename.new

you can execute a mv in similar loop to replace old files with new ones

ls -R | while read i
do
     sed -e 's/abc/xyz/g' $i > $i.new
     mv -f $i.new $i
done
0
 
LVL 7

Accepted Solution

by:
glassd earned 50 total points
ID: 9714437
Alternatives.

Use find to get the file list:

for File in $(find . -type f -print)
do
   ...
done

Use ed to edit the files in place. No separate output file created:

ed - $File <<- EOF
g/abc/s/abc/xyz/g
w
EOF
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vaibhavmishra062201
ID: 9714863
hi sunnycoder

it is creating al the .new files in current folder ...
can you please modify the script so that .new file gets created in the folder same as the original file .. ?

Vaibhav
0
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 75 total points
ID: 9714906
riding on glassd's suggestion of using find

for i in `find . -type f -print`
do
    sed -e 's/abc/xyz/g' $i > $i.new
    mv -f $i.new $i
done

0
 

Author Comment

by:vaibhavmishra062201
ID: 9715426
ok ... i ran it on a test folder and it is working fine ..

do you want to share the points with glassd ? if yes how many ?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 9718401
I will add my usual comment about checking the status before renaming the file, otherwise you can end up nobbling loads of files

sed 's/abc/xyz/g' $i >$i.new && mv $i.new $i
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9720077
>do you want to share the points with glassd ? if yes how many ?
you have to decide if you want to split and the ratio too
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question