Home Man
Search
Today's Posts
Register

This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in the UNIX for Beginners Questions & Answers forum.

best method of replacing multiple strings in multiple files - sed or awk? most simple preferred :)

Tags
beginners

 

 
Thread Tools Search this Thread
# 1  
Old 08-25-2010
Computer best method of replacing multiple strings in multiple files - sed or awk? most simple preferred :)

Hi guys,

say I have a few files in a directory (58 text files or somthing)

each one contains mulitple strings that I wish to replace with other strings

so in these 58 files I'm looking for say the following strings:

JAM (replace with BUTTER)
BREAD (replace with CRACKER)
SCOOP (replace with FORK)
SPREAD (replace with SPLAT)
.....
etc etc

what's best for this? sed or awk?

I'd like to make it quick as possible
# 2  
Old 08-25-2010
sed is OK for that task. Perl could be good too (with -i switch).
Code:
sed -i 's/JAM/BUTTER/g;s/BREAD/CRACKER/g;s/SCOOP/FORK/g;s/SPREAD/SPLAT/g' *

# 3  
Old 08-26-2010
to loop through all the files in a dir would the following work?:

Code:
function Replace
{
for f in $files
do

 sed -i 's/JAM/BUTTER/g;s/BREAD/CRACKER/g;s/SCOOP/FORK/g;s/SPREAD/SPLAT/g' *
    
exit
    
done
}

# 4  
Old 08-26-2010
That command alone will replace those strings in all files in current directory. This is because there is shell globbing operator "*" as the argument for sed command. Did you try that command on some test files? Is it not working properly?
# 5  
Old 08-26-2010
try this

Code:
function Replace
{
for f in $(ls -1)
 do

sed -i 's/JAM/BUTTER/g;s/BREAD/CRACKER/g;s/SCOOP/FORK/g;s/SPREAD/SPLAT/g' "$f"    
exit
  
 done
}


Last edited by ygemici; 08-26-2010 at 12:43 PM..
# 6  
Old 08-26-2010
set a init file as: (So you can have a long list)

Code:
$ cat template

JAM BUTTER
BREAD CRACKER
SCOOP FORK
SPREAD SPLAT
......

Code:
for file in $(ls *.txt)
do
   awk 'NR==FNR {a[$1]=$2;next} {for ( i in a) gsub(i,a[i])}1' template $file >temp.txt
   mv  temp.txt $file
done


Last edited by rdcwayx; 08-26-2010 at 06:51 AM..
The Following User Says Thank You to rdcwayx For This Useful Post:
yifangt (11-03-2017)
# 7  
Old 08-26-2010
what do you mean rdcwayx? can you explain? cheers

---------- Post updated at 02:21 PM ---------- Previous update was at 01:31 PM ----------

Code:
function Replace
{
for f in $(ls -1)do

sed -i 's/JAM/BUTTER/g;s/BREAD/CRACKER/g;s/SCOOP/FORK/g;s/SPREAD/SPLAT/g' "$f"    
exit
    
done
}

throws the following error in OpenSolaris 10:

line 37: syntax error near unexpected token `sed'
 

« 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
sed parser behaving strange on replacing multiple words in multiple files sammy777888 Shell Programming and Scripting 1 12-08-2017 05:02 AM
How to pass strings from a list of strings from another file and create multiple files? nubie2linux UNIX for Beginners Questions & Answers 2 04-10-2017 08:37 AM
Grep strings on multiple files and output to multiple files am24 Shell Programming and Scripting 4 04-20-2016 02:17 AM
Replacing old TNS entries with New one in multiple files KDDubai333 Shell Programming and Scripting 4 10-04-2014 04:21 AM
Grep multiple strings in multiple files xshang Shell Programming and Scripting 7 11-05-2012 01:01 PM
Search & Replace: Multiple Strings / Multiple Files spacegoose Shell Programming and Scripting 2 10-16-2012 08:17 PM
Grep multiple strings in multiple files using single command gagan4599 UNIX for Dummies Questions & Answers 2 01-19-2011 06:22 AM
grep command to find multiple strings in multiple lines in a file. ritikaSharma UNIX for Dummies Questions & Answers 2 12-17-2010 03:43 AM
Replacing text from multiple files at multiple location rudoraj Shell Programming and Scripting 2 05-12-2010 04:16 PM
Replacing string in multiple files arulanandsp Shell Programming and Scripting 3 09-11-2009 12:35 AM


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

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password