|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
> 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 | ||
|
![]() |
| Tags |
| open files, piping, substitution |
| Thread Tools | Search this Thread |
| 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 |
|
|