Unix/Linux Go Back    


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

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

UNIX for Dummies Questions & Answers


 
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-25-2010
rich@ardz rich@ardz is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 22 February 2012, 5:05 AM EST
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old Unix and Linux 08-25-2010
bartus11's Unix or Linux Image
bartus11 bartus11 is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 3 August 2016, 11:03 AM EDT
Posts: 3,733
Thanks: 7
Thanked 1,152 Times in 1,122 Posts
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' *

Sponsored Links
    #3  
Old Unix and Linux 08-26-2010
rich@ardz rich@ardz is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 22 February 2012, 5:05 AM EST
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
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 Unix and Linux 08-26-2010
bartus11's Unix or Linux Image
bartus11 bartus11 is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 3 August 2016, 11:03 AM EDT
Posts: 3,733
Thanks: 7
Thanked 1,152 Times in 1,122 Posts
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?
Sponsored Links
    #5  
Old Unix and Linux 08-26-2010
ygemici ygemici is offline Forum Advisor  
sed_shell@LNU
 
Join Date: Feb 2010
Last Activity: 26 April 2017, 8:59 AM EDT
Location: istanbul
Posts: 1,713
Thanks: 4
Thanked 295 Times in 286 Posts
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..
Sponsored Links
    #6  
Old Unix and Linux 08-26-2010
rdcwayx rdcwayx is offline Forum Advisor  
Use nawk in Solaris
 
Join Date: Jun 2006
Last Activity: 13 September 2015, 8:58 PM EDT
Posts: 2,759
Thanks: 44
Thanked 419 Times in 407 Posts
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..
Sponsored Links
    #7  
Old Unix and Linux 08-26-2010
rich@ardz rich@ardz is offline
Registered User
 
Join Date: Apr 2010
Last Activity: 22 February 2012, 5:05 AM EST
Posts: 52
Thanks: 12
Thanked 0 Times in 0 Posts
what do you mean rdcwayx? can you explain? cheers Linux

---------- 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'
Sponsored Links
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
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
need help with post:extract multiple columns from multiple files manishabh Shell Programming and Scripting 0 08-19-2009 01:09 PM
extract multiple cloumns from multiple files; skip rows and include filenames; awk manishabh Shell Programming and Scripting 4 08-18-2009 10:55 PM
renaming multiple files while replacing string kevin9 Shell Programming and Scripting 2 05-10-2009 06:41 PM



All times are GMT -4. The time now is 09:59 PM.