Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Find and Replace in multiple files (Shell script)

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-20-2002
jatins_s jatins_s is offline
Registered User
 
Join Date: Aug 2002
Last Activity: 3 September 2002, 7:05 AM EDT
Location: India
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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 ???

jatin

Last edited by jatins_s; 08-20-2002 at 02:27 AM..
Sponsored Links
    #2  
Old Unix and Linux 08-20-2002
asifraj's Unix or Linux Image
asifraj asifraj is offline
Registered User
 
Join Date: Aug 2002
Last Activity: 17 October 2003, 10:33 PM EDT
Location: India
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
hi,

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
asif
Sponsored Links
    #3  
Old Unix and Linux 08-21-2002
oombera's Unix or Linux Image
oombera oombera is offline Forum Advisor  
Registered User
 
Join Date: Aug 2002
Last Activity: 3 October 2004, 6:07 PM EDT
Location: Cleveland, OH
Posts: 804
Thanks: 0
Thanked 6 Times in 5 Posts
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" ]
then
TMP=./tmpfile_$$
sed -e 's/ABCD/DCBA/g' < $i > $TMP
mv $TMP $i
else
echo "The file $i does not include the word ABCD"
fi
done

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 Unix and Linux 08-21-2002
peter.herlihy peter.herlihy is offline
Registered User
 
Join Date: Nov 2001
Last Activity: 1 August 2006, 11:51 AM EDT
Location: New Zealand
Posts: 333
Thanks: 0
Thanked 0 Times in 0 Posts
This is a bit simplier.

for y in `ls *`;
do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y;
done
Sponsored Links
    #5  
Old Unix and Linux 08-22-2002
asifraj's Unix or Linux Image
asifraj asifraj is offline
Registered User
 
Join Date: Aug 2002
Last Activity: 17 October 2003, 10:33 PM EDT
Location: India
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #6  
Old Unix and Linux 08-22-2002
LivinFree's Unix or Linux Image
LivinFree LivinFree is offline Forum Advisor  
Goober Extraordinaire
 
Join Date: Jul 2001
Last Activity: 16 June 2011, 4:50 PM EDT
Location: Portland, OR, USA
Posts: 1,626
Thanks: 2
Thanked 15 Times in 13 Posts
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...
Sponsored Links
    #7  
Old Unix and Linux 08-22-2002
asifraj's Unix or Linux Image
asifraj asifraj is offline
Registered User
 
Join Date: Aug 2002
Last Activity: 17 October 2003, 10:33 PM EDT
Location: India
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
MySQL

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.
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
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
how to find and replace strings in multiple files etldeveloper UNIX for Dummies Questions & Answers 0 02-11-2011 05:00 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



All times are GMT -4. The time now is 10:47 PM.