substitution to mulitple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting substitution to mulitple files
# 1  
Old 10-10-2012
substitution to mulitple files

Hello all,

I am trying to make a script that will apply a substitution to any number of files given on the command line.
Example would be

~/Unix/script/subst car boat myFile1.txt myFile2.txt myFile3.txt

This is the code I have so far but it does not function as needed.
Code:
PAT=$1
shift
REPL=$1
shift

for FILE in $*
do
mv $3 $3.bak
sed "s/$1/$2/g" $3.bak > $3
done

Looking for any pointers.

Thanks
# 2  
Old 10-10-2012
Code:
PAT=$1
shift
REPL=$1
shift

for FILE in $*
do
mv $FILE $FILE.bak
sed "s/$1/$2/g" $FILE.bak > $FILE
done

# 3  
Old 10-10-2012
You almost had it... You saved the pattern and replacement strings, but instead of using those saved values in your call to sed, you used $1 and $2 after shifting them away... Similarly, you used $3 instead of $FILE. The suggestion from rdrtx1 fixed the last issue, but not the first. Try:
Code:
PAT=$1
shift
REPL=$1
shift

for FILE in $*
do
mv $FILE $FILE.bak
sed "s/$PAT/$REPL/g" $FILE.bak > $FILE
done

Note that if there could be hard links to any of your $FILE operands or if the permissions on $FILE aren't the current default for newly created files, you should consider using cp instead of mv when creating your backup files (but you will need to be sure that the current permissions allow you to write the file).
# 4  
Old 10-10-2012
Actually, that is closer to the results I am looking to but they still are not exact.
Example
Code:
~/Unix/script/subst "hi guys" "hello everyone" myFile1.txt myFile2.txt myFile3.txt

and I want it to make that replacement in everyfile. my code works for just one file, but I am having some trouble manipulating the loop to make it work for multiple files.

Last edited by ramn214; 10-11-2012 at 12:03 AM..
# 5  
Old 10-11-2012
When I created the following three files:
f1:
Code:
cat calico
dog dachsund

f2:
Code:
cat tabby
dog collie

f3:
Code:
cat siamese
dog retriever

and stored the following in a file named [icode]tester[icode]:
Code:
#!/bin/ksh
set -xv
PAT=$1
shift
REPL=$1
shift

for FILE in $*
do
        mv $FILE $FILE.bak
        sed "s/$PAT/$REPL/g" $FILE.bak > $FILE
done

and made tester executable by:
Code:
chmod +x tester

and executed it with the command line:
Code:
tester cat kitty f?

the tracing provided clearly shows that cat appears in the sed search field and kitty appears in the sed replacement field while editting f1, f2, and f3. All of the original files are preserved with the .bak extension and all of the original file names now contain kitty instead of cat.

What wasn't working for you?
# 6  
Old 10-11-2012
What would I do if I am not just using .bak files such as .dat or .foo ?
# 7  
Old 10-11-2012
Quote:
Originally Posted by ramn214
What would I do if I am not just using .bak files such as .dat or .foo ?
I don't understand the question. You said you're using the script I provided and it isn't working. If you're saying that you changed .bak to .dat or to .foo in both places where it appears in the script, it shouldn't make any difference. If you're saying you changed:
Code:
        mv $FILE $FILE.bak
        sed "s/$PAT/$REPL/g" $FILE.bak > $FILE

to:
Code:
        sed "s/$PAT/$REPL/g" $FILE > $FILE

then you have thrown away all of your data and created empty files in their place.

If you're saying that you're running on DOS that only allows a single "." to appear in a filename and moving x.dat to x.dat.bak isn't working; you should have told us up front that even though you're posting in the UNIX and Linux forums, you're not running on a UNIX-, Linux-, or BSD-system and have a much tighter set of constraints not placed on applications using standard utilities in a roughly POSIX conforming environment.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk comparision between 2 files and substitution in third

Hi All, I have two files in the following format. File 1 : 1044|1|20121031|2910039.4|MR|201210|G1044|E 1082|2|20121031|1664662.84|MR|201210|G1082|E 1696|3|20121031|190801.5|MR|201210|G1696|E 1824|4|20121031|196350|MR|201210|G1824|E 1900|5|20121031|221447.8|MR|201210|G1900|E File 2 :... (7 Replies)
Discussion started by: nua7
7 Replies

2. UNIX for Advanced & Expert Users

FTP failed to copy mulitple files from multiple directory

I am using below scripts to copy all the files from multiple folders. By executing individually command i am able to copy all the files but using scripts only getting first file. System is ignoring the second CD and mget command. HOST=server.com USER=loginid PASSWD="abc" echo "open $HOST... (6 Replies)
Discussion started by: meetvipin
6 Replies

3. UNIX for Dummies Questions & Answers

mulitple grep using tail

I have a basic tail/grep question. I have logs that are generated & kept in a directory called alert_audit. I am using "tail" to see the logs that are coming in, but I only need logs that contain the IP address 10.249.185. or 10.247.231. Here is the command I have, but it pulls all IP... (3 Replies)
Discussion started by: robertson1995
3 Replies

4. Shell Programming and Scripting

Help with Find/Replace Javascript Injected Strings in mulitple files

Hi, guys, I'm not a high-end programmer, but I've been trying to write a script to remove all of the b.rtbn2.cn (and b.adserv.cn and any future variation) injected script tags on the server. (Still working on security fixes to prevent it in the future, just need to clean up now.) My approach is... (1 Reply)
Discussion started by: zzlegs
1 Replies

5. Shell Programming and Scripting

Opening Mulitple files using For loop in Perl

Hi All, I have a total of ten file to open in the Perl script and i am using a for loop to open each file and capture some strings inside each file. Unfortunately, i encounter the below syntax error. I think there should be something wrong with this term reports_${counting}_${_}.txt but i do... (4 Replies)
Discussion started by: Raynon
4 Replies

6. Shell Programming and Scripting

Need help on Mulitple files mutliple actions

Hi all, I have mistkanely gzipped twice an entire folder and sub folders, and also renamed the files during that process. I am trying to undo this, and I need help to create the batch to work on it. All folders are under my images directory, I have a output.txt file that holds all the... (1 Reply)
Discussion started by: saariko
1 Replies

7. Linux

Multiple files and word substitution

Hi , I have bunch of sql file which contain UHCDEV01 in them . I want to replace all the UHCDEV01 with UHCETL01 in all the files. I have written this code which shows correct output on sh -x but doesn't change the output file . #cat change_dbname.shl #!/bin/ksh... (1 Reply)
Discussion started by: capri_drm
1 Replies

8. Shell Programming and Scripting

awk with mulitple FS

Hi, I would like to get 5th field from this output using FS. Filesystem 1024-blocks Used Available Capacity Mounted on 172.29.138.222:/vol/vol0 311936256 2565248 309371008 1% /tmp/test My command is df -kP | awk ' BEGIN { FS="" ; } { print $5 ; } ' For some... (5 Replies)
Discussion started by: phamp008
5 Replies

9. UNIX for Dummies Questions & Answers

printing to mulitple printers

I want to send a single report from my application to mulitple printers with one command. I tried using a synomym and assigning it to different printers, but it stops when it finds the first hit. Has anyone done this at the unix level through printcap? (2 Replies)
Discussion started by: MizzGail
2 Replies

10. UNIX for Dummies Questions & Answers

Update text files in place (string substitution) ??

The auditors have nailed us for world writeable files.... Apparently in years gone by, quite a number of our kornshell scripts have had: umask 000 put in the script. We have been able to turn off world writeable for existing dirs & files, but as these scripts run, new files keep getting... (1 Reply)
Discussion started by: kornshellmaven
1 Replies
Login or Register to Ask a Question