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
    #8  
Old Unix and Linux 08-26-2010
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 27 June 2017, 3:46 AM EDT
Location: Zürich
Posts: 8,616
Thanks: 346
Thanked 1,106 Times in 956 Posts
Hi.

Your problem is here:


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

Should be:


Code:
for f in $(ls -1); do

or:

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

But it's not much use if the filenames contain spaces.


Code:
function Replace
{
  ls | while read f; do
    sed -i 's/JAM/BUTTER/g;s/BREAD/CRACKER/g;s/SCOOP/FORK/g;s/SPREAD/SPLAT/g' "$f"
  done
}

Sponsored Links
    #9  
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

Code:
function Replace
{
  ls | while read f; do
    sed -i 's/JAM/BUTTER/g;s/BREAD/CRACKER/g;s/SCOOP/FORK/g;s/SPREAD/SPLAT/g' "$f"
  done
}

cheers for the help

ran the code above and it throws:

sed: illegal option -- i

there is just one file in the dir called 'test.log'? no spaces in the file name?

How would you get round the problem of having spaces in the filenames? assuming you wanted to run it against files with spaces in the name?
Sponsored Links
    #10  
Old Unix and Linux 08-26-2010
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 27 June 2017, 3:46 AM EDT
Location: Zürich
Posts: 8,616
Thanks: 346
Thanked 1,106 Times in 956 Posts
Hi.

Then you can either use ed, or write to a temporary file:


Code:
function Replace
{
  ls | while read f; do
    sed 's/JAM/BUTTER/g;s/BREAD/CRACKER/g;s/SCOOP/FORK/g;s/SPREAD/SPLAT/g' "$f" > "$f".tmp && cp -f "$f".tmp "$f" && rm "$f".tmp
  done
}

To handle filenames with spaces, don't use a for-loop. Go with the while-loop.

Last edited by Scott; 08-26-2010 at 11:19 AM.. Reason: Removed wrong file
The Following User Says Thank You to Scott For This Useful Post:
rich@ardz (08-26-2010)
    #11  
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
amazing! works lovely - it removes the original text file and instead just leaves:

test.log.tmp

in the dir - is is meant to do that?

I love Unix Linux

it seems to remove the string 'SPREAD' completely? i.e.

orginal:

1 JAM
2 BREAD
3 SCOOP
4 SPREAD

open file after running sed:

1 BUTTER
2 CRACKER
3 FORK
4

I.e. SPLAT is missing from line 4?

Last edited by rich@ardz; 08-26-2010 at 11:19 AM.. Reason: additional info
Sponsored Links
    #12  
Old Unix and Linux 08-26-2010
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 27 June 2017, 3:46 AM EDT
Location: Zürich
Posts: 8,616
Thanks: 346
Thanked 1,106 Times in 956 Posts
Quote:
Originally Posted by rich@ardz View Post
amazing! works lovely - it removes the original text file and instead just leaves:

test.log.tmp

in the dir - is is meant to do that?

I love Unix Linux
Um, no!

Sorry my mistake. I've updated my previous post Linux
The Following User Says Thank You to Scott For This Useful Post:
rich@ardz (08-26-2010)
Sponsored Links
    #13  
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
Looking good Linux the sed seems to completely remove the final line of the txt file however?
Sponsored Links
    #14  
Old Unix and Linux 08-26-2010
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 27 June 2017, 3:46 AM EDT
Location: Zürich
Posts: 8,616
Thanks: 346
Thanked 1,106 Times in 956 Posts
Quote:
Originally Posted by rich@ardz View Post
Looking good Linux the sed seems to completely remove the final line of the txt file however?
Hi.

I looked at that, and it seems to work for me.


Code:
$ cat file1
1 JAM
2 BREAD
3 SCOOP
4 SPREAD

$ sed 's/JAM/BUTTER/g;s/BREAD/CRACKER/g;s/SCOOP/FORK/g;s/SPREAD/SPLAT/g' file1
1 BUTTER
2 CRACKER
3 FORK
4 SPLAT

My first thought was a missing newline on the last line, but I simulated that and it still worked.

Does this show one fewer line than is in your file?:

Code:
wc -l filename

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 04:53 PM.