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 03:27 AM..
# 2  
Old 08-20-2002

try this,

cd <your_folder>
ls >


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

save and exit


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 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 11: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

Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads 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 05:14 AM
Find and replace in multiple files ashokvpp Shell Programming and Scripting 1 05-04-2018 08:45 AM
Shell script to find and replace contents of files in directory theprogrammer Shell Programming and Scripting 2 04-05-2013 01: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 10:05 AM
how to find files and replace them in a directory in Shell scripting pratima.kumari Shell Programming and Scripting 6 11-29-2011 01:09 PM
linux find and replace in multiple files cokedude UNIX for Advanced & Expert Users 2 09-14-2011 08:55 AM
Find, replace, file path in multiple files for Solaris 10 LRoberts Emergency UNIX and Linux Support 5 04-21-2011 10:31 AM
how to find and replace strings in multiple files etldeveloper UNIX for Dummies Questions & Answers 0 02-11-2011 06:00 AM
Script to multiple find and replace in a single file wildhorse Shell Programming and Scripting 6 04-20-2010 11:21 AM
awk find and replace in multiple files jdhahbi Shell Programming and Scripting 7 07-22-2009 11:03 AM
Find and replace in all files using shell scripting slothario UNIX for Dummies Questions & Answers 11 03-24-2009 03:43 PM
shell script to find and replace string in multiple files pharos467 Shell Programming and Scripting 11 01-15-2009 11:40 AM
Shell Script to replace tokens in multiple files cbo0485 Shell Programming and Scripting 20 11-20-2008 07:52 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 03:00 AM
All times are GMT -4. The time now is 08:11 PM.

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

Not a Forum Member?
Forgot Password?