The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
shell script to find and replace string in multiple files pharos467 Shell Programming and Scripting 11 01-15-2009 11:40 AM
Executing Multiple .SQL Files from Single Shell Script file anushilrai Shell Programming and Scripting 3 04-07-2008 10:09 AM
Find and replace a string in multiple files pharos467 UNIX for Dummies Questions & Answers 2 11-05-2007 11:47 PM
Find and replace files in multiple folders lodey Shell Programming and Scripting 6 09-28-2007 02:00 AM
find and FTP multiple files in Korn Shell lambjam UNIX for Dummies Questions & Answers 2 08-13-2007 10:50 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-20-2002
jatins_s jatins_s is offline
Registered User
  
 

Join Date: Aug 2002
Location: India
Posts: 2
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..
  #2 (permalink)  
Old 08-20-2002
asifraj's Avatar
asifraj asifraj is offline
Registered User
  
 

Join Date: Aug 2002
Location: India
Posts: 48
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
  #3 (permalink)  
Old 08-21-2002
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
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 (permalink)  
Old 08-21-2002
peter.herlihy peter.herlihy is offline
Registered User
  
 

Join Date: Nov 2001
Location: New Zealand
Posts: 333
This is a bit simplier.

for y in `ls *`;
do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y;
done
  #5 (permalink)  
Old 08-22-2002
asifraj's Avatar
asifraj asifraj is offline
Registered User
  
 

Join Date: Aug 2002
Location: India
Posts: 48
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 (permalink)  
Old 08-22-2002
LivinFree's Avatar
LivinFree LivinFree is offline Forum Advisor  
Goober Extraordinaire
  
 

Join Date: Jul 2001
Location: Portland, OR, USA
Posts: 1,584
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 (permalink)  
Old 08-22-2002
asifraj's Avatar
asifraj asifraj is offline
Registered User
  
 

Join Date: Aug 2002
Location: India
Posts: 48
Thumbs up

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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0