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


Tags
open files, piping, substitution

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
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
    #2  
Old 01-25-2013
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Gotham Knight
 
Join Date: Mar 2012
Last Activity: 15 April 2014, 12:52 PM EDT
Location: India
Posts: 1,412
Thanks: 100
Thanked 495 Times in 472 Posts

Code:
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
    #3  
Old 01-25-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 15 April 2014, 5:23 PM EDT
Location: Saskatchewan
Posts: 18,516
Thanks: 680
Thanked 3,034 Times in 2,854 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.

Code:
# 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
do
        sed 's/..../' "$FILENAME" > /tmp/$$
        cat /tmp/$$ > "$FILENAME"
done

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 12:06 PM
Multiple variable substitutions koondog Shell Programming and Scripting 2 10-20-2010 01: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 03: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 10:15 AM
Using Sed to perform multiple substitutions? Glyn_Mo Shell Programming and Scripting 4 04-15-2010 01:22 PM



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