Solved

replace string in all the files

Posted on 2003-11-10
8
661 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
8 Comments
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:vaibhavmishra062201
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 75 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now