Home Man
Today's Posts

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Find and Replace in multiple files (Shell script)

find, multiple files, script, shell script, shell scripts, unix

Login to Reply

Thread Tools Search this Thread
# 1  
Old 08-20-2002
Find and Replace in multiple files (Unix Shell script)

hi guys,

Suppose you have 100 files in a folder and you want to replace all occurances of a word say "ABCD" in those files with "DCBA", how would you do
it ???


Last edited by jatins_s; 08-20-2002 at 02:27 AM..
# 2  
Old 08-20-2002

try this,

cd <your_folder>
ls > out.sh

vi out.sh

:%s/^/perl -pi -e 's\/ABCD\/DCBA\/' /

save and exit

sh out.sh

and bingo....

"perl -pi -e 's/abcd/dcba/' filename" will basically do the substitution and listing all the files in a file then prepending this string in front of each filename, will produce a shell script out.sh which would run this perl command on each of the files.

hope it helps
# 3  
Old 08-21-2002
If I understand what you're asking, this code should do what you want (ksh):

cd your_pathname
for i in `lx`; do
FOUND=`grep ABCD $i`
if [ "$FOUND" ]
sed -e 's/ABCD/DCBA/g' < $i > $TMP
mv $TMP $i
echo "The file $i does not include the word ABCD"

This script reads each file in your_pathname, searches for the string ABCD, and either replaces it with DCBA or alerts you that the string ABCD was not found.

Last edited by oombera; 08-21-2002 at 10:49 AM..
# 4  
Old 08-21-2002
This is a bit simplier.

for y in `ls *`;
do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y;
# 5  
Old 08-22-2002
well there are so many different ways of doing it.

but what i am suggesting is why unnecessary create a temporary file when you can do without.

also, any suggestions from anyone, which of the above mentioned methods would be fastest.
# 6  
Old 08-22-2002
I would bet that peter's is faster...

You could make it a split second faster by getting rid of the `ls *` though...

If could just be:
for y in *
since the shell will expand the * into all of the file names...
If you had directories in there, you'd want to use "find" to get only the files...
# 7  
Old 08-22-2002

remember you are redirecting output to a file and then moving the file back to the original
what if the file sizes are of the order of tens of MBs.
secondly, the shell script would compulsorily do it for all the files in the directory.
but while using a file, you can customize your renaming process.
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find and replace from multiple files ninocap UNIX for Beginners Questions & Answers 2 06-12-2018 04:14 AM
Find and replace in multiple files ashokvpp Shell Programming and Scripting 1 05-04-2018 07:45 AM
Shell script to find and replace contents of files in directory theprogrammer Shell Programming and Scripting 2 04-05-2013 12:27 AM
Script to find & replace a multiple lines string across multiple php files and subdirectories spfc_dmt Shell Programming and Scripting 12 03-07-2012 09:05 AM
linux find and replace in multiple files cokedude UNIX for Advanced & Expert Users 2 09-14-2011 07:55 AM
awk find and replace in multiple files jdhahbi Shell Programming and Scripting 7 07-22-2009 10:03 AM
shell script to find and replace string in multiple files pharos467 Shell Programming and Scripting 11 01-15-2009 10:40 AM
Shell Script to replace tokens in multiple files cbo0485 Shell Programming and Scripting 20 11-20-2008 06:52 AM
Find and replace a string in multiple files pharos467 UNIX for Dummies Questions & Answers 2 11-05-2007 10:47 PM
Find and replace files in multiple folders lodey Shell Programming and Scripting 6 09-28-2007 02:00 AM

All times are GMT -4. The time now is 05:22 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
Show Password