Multiple Substitutions across Multiple Files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple Substitutions across Multiple Files
# 1  
Old 01-25-2013
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!
# 2  
Old 01-25-2013
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.
This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 01-25-2013
> 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/$$

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Export Oracle multiple tables to multiple csv files using UNIX shell scripting

Hello All, just wanted to export multiple tables from oracle sql using unix shell script to csv file and the below code is exporting only the first table. Can you please suggest why? or any better idea? export FILE="/abc/autom/file/geo_JOB.csv" Export= `sqlplus -s dev01/password@dEV3... (16 Replies)
Discussion started by: Hope
16 Replies

2. Shell Programming and Scripting

sed parser behaving strange on replacing multiple words in multiple files

I have 4000 files like $cat clus_grp_seq10_g.phy 18 1002 anig_OJJ65951_1 ATGGTTTCGCAGCGTGATAGAGAATTGTTTAGGGATGATATTCGCTCGCGAGGAACGAAGCTCAATGCTGCCGAGCGCGAGAGTCTGCTAAGGCCATATCTGCCAGATCCGTCTGACCTTCCACGCAGGCCACTTCAGCGGCGCAAGAAGGTTCCTCG aver_OOF92921_1 ... (1 Reply)
Discussion started by: sammy777888
1 Replies

3. Shell Programming and Scripting

Removing carriage returns from multiple lines in multiple files of different number of columns

Hello Gurus, I have a multiple pipe separated files which have records going over multiple Lines. End of line separator is \n and records going over multiple lines have <CR> as separator. below is example from one file. 1|ABC DEF|100|10 2|PQ RS T|200|20 3| UVWXYZ|300|30 4| GHIJKL|400|40... (7 Replies)
Discussion started by: dJHa
7 Replies

4. UNIX for Dummies Questions & Answers

Multiple substitutions in one expression using sed

Hi, I'm trying to get multiple substitutions in one expression using sed: echo "-foo-_-bar--foo-_bar_-_foo_bar_-foo_-_bar_-" | sed -e "s//-/g" So, as you can see I'm trying to replace all instances of _-, -_, -- with - (dash) I have provided bad example. The question is how to use multiple... (6 Replies)
Discussion started by: useretail
6 Replies

5. Shell Programming and Scripting

Create Multiple UNIX Files for Multiple SQL Rows output

Dear All, I am trying to write a Unix Script which fires a sql query. The output of the sql query gives multiple rows. Each row should be saved in a separate Unix File. The number of rows of sql output can be variable. I am able save all the rows in one file but in separate files. Any... (14 Replies)
Discussion started by: Rahul_Bhasin
14 Replies

6. Programming

Control multiple program instances - open multiple files problem

Hello. This shouldn't be an unusual problem, but I cannot find anything about it at google or at other search machine. So, I've made an application using C++ and QtCreator. I 've made a new mime type for application's project files. My system (ubuntu 10.10), when I right click a file and I... (3 Replies)
Discussion started by: hakermania
3 Replies

7. Shell Programming and Scripting

Multiple variable substitutions

Is there anyway to accomplish this? (ksh) FILES_TO_PROCESS='NAME1 NAME2' SOURCE_NAME1=/tmp/myfile TARGET_NAME1=/somewhere/else # other file names for i in $FILES_TO_PROCESS do file1=SOURCE_$i file2=TARGET_$i echo cp ${$file1} ${$file2} <-- how do get this to work. done (2 Replies)
Discussion started by: koondog
2 Replies

8. UNIX for Dummies Questions & Answers

Using AWK: Extract data from multiple files and output to multiple new files

Hi, I'd like to process multiple files. For example: file1.txt file2.txt file3.txt Each file contains several lines of data. I want to extract a piece of data and output it to a new file. file1.txt ----> newfile1.txt file2.txt ----> newfile2.txt file3.txt ----> newfile3.txt Here is... (3 Replies)
Discussion started by: Liverpaul09
3 Replies

9. UNIX for Dummies Questions & Answers

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... (19 Replies)
Discussion started by: rich@ardz
19 Replies

10. Shell Programming and Scripting

Using Sed to perform multiple substitutions?

Hello I have the following output which is returned with the Month in text format instead of numerical. The output I receive is performed by using Rational Synergy CM software commands from the Unix command line and piping Unix commands on the end. bash-3.00$ ccm query -n... (4 Replies)
Discussion started by: Glyn_Mo
4 Replies
Login or Register to Ask a Question