Multiple Substitutions across Multiple Files | Unix Linux Forums | Shell Programming and Scripting

  Go Back    

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

Multiple Substitutions across Multiple Files

Shell Programming and Scripting

open files, piping, substitution

Closed Thread    
Thread Tools Search this Thread Display Modes
Old 01-25-2013
Alexander4444 Alexander4444 is offline
Registered User
Join Date: Apr 2010
Last Activity: 25 January 2013, 12:43 PM EST
Posts: 12
Thanks: 6
Thanked 0 Times in 0 Posts
Multiple Substitutions across Multiple Files

Hey everyone! I am determining the best method to do what the subject of this thread says. I only have pieces to the puzzle right now. Namely this:

grep -rl "expression" . | xargs open

(I should mention that the intention is to grep through many files containing the "expression" and return the files themselves for subsequent editing.)

and this:

... | for line in source; do sed 's/expression/replacement/g' > tmp; done

Except that the issue is how to open each file, substitute, and save to the same respective files, not just save to one big tmp file. :P This is eluding me. I realize:

mv tmp > original_file

can be done for each case, but this seems to require a level of scripting knowledge I currently lack. Thanks for any help/suggestions/advice on this!
Sponsored Links
Old 01-25-2013
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
Join Date: Mar 2012
Last Activity: 9 October 2014, 4:50 PM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts

perl -i.bak -pe 's/this/that/g' <list of file names>

Remove the .bak files after verifying that the substitutions have been done properly.

Note : This will change the inode numbers of all the files.
The Following User Says Thank You to elixir_sinari For This Useful Post:
Alexander4444 (01-25-2013)
Sponsored Links
Old 01-25-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
Join Date: Aug 2005
Last Activity: 24 January 2015, 12:58 AM EST
Location: Saskatchewan
Posts: 20,161
Thanks: 862
Thanked 3,500 Times in 3,268 Posts
> does not append, > overwrites. So, for each file, overwrite the temp file, then overwrite the original.

If you have GNU sed, you can sed -i to edit in-place instead of cat-ing the new file atop the old.

I don't like the look of this, editing your originals is dangerous, so as a first step BACK UP YOUR FILES. One program bug could wipe out the data of interest here.

# Step 1:  Back up your files.  Editing your originals is dangerous!
grep -rl "expression" . | xargs tar -zcf backup-$(date +%Y-%m-%d-%H-%M-%S).tar.gz

# Step 2:  Find the files, read them in order, substitute.
grep -rl "expression" | while read FILENAME
        sed 's/..../' "$FILENAME" > /tmp/$$
        cat /tmp/$$ > "$FILENAME"

rm -f /tmp/$$

The Following User Says Thank You to Corona688 For This Useful Post:
Alexander4444 (01-25-2013)
Sponsored Links
Closed 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
Control multiple program instances - open multiple files problem hakermania Programming 3 02-06-2011 01:06 PM
Multiple variable substitutions koondog Shell Programming and Scripting 2 10-20-2010 02:24 PM
Using AWK: Extract data from multiple files and output to multiple new files Liverpaul09 UNIX for Dummies Questions & Answers 3 10-12-2010 04:59 AM
best method of replacing multiple strings in multiple files - sed or awk? most simple preferred :) rich@ardz UNIX for Dummies Questions & Answers 19 08-27-2010 11:15 AM
Using Sed to perform multiple substitutions? Glyn_Mo Shell Programming and Scripting 4 04-15-2010 02:22 PM

All times are GMT -4. The time now is 06:24 AM.