substitution to mulitple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting substitution to mulitple files
# 8  
Old 10-11-2012
I am in a Unix environment. Basically this is the error I am getting.
Code:
+ REPL='U N I X' 
shift
+ shift

for FILE in $*
do
        mv $FILE $FILE.txt
        sed "s/$PAT/$REPL/g" $FILE.txt > $FILE
done
+ FILE=_king
+ mv _king _king.txt
+ sed 's/Unix/U N I X/g' _king.txt
+ FILE=cobra.dat
+ mv cobra.dat cobra.dat.txt
+ sed 's/Unix/U N I X/g' cobra.dat.txt
+ FILE=_ne$wt.foo
+ mv '_ne$wt.foo' '_ne$wt.foo.txt'
+ sed 's/Unix/U N I X/g' '_ne$wt.foo.txt'

# 9  
Old 10-11-2012
Quote:
Originally Posted by ramn214
I am in a Unix environment. Basically this is the error I am getting.
Code:
+ REPL='U N I X' 
shift
+ shift

for FILE in $*
do
        mv $FILE $FILE.txt
        sed "s/$PAT/$REPL/g" $FILE.txt > $FILE
done
+ FILE=_king
+ mv _king _king.txt
+ sed 's/Unix/U N I X/g' _king.txt
+ FILE=cobra.dat
+ mv cobra.dat cobra.dat.txt
+ sed 's/Unix/U N I X/g' cobra.dat.txt
+ FILE=_ne$wt.foo
+ mv '_ne$wt.foo' '_ne$wt.foo.txt'
+ sed 's/Unix/U N I X/g' '_ne$wt.foo.txt'

I apologize for my careless programming. I was lured in by your example with filenames myFile1.txt, myFile2.txt, and myFile3.txt. Having a <dollar-sign> in a filename is a completely different game. Please try again with this script:
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

I'm assuming that you don't have a shell variable named wt so an unquoted expansion of _ne$wt.foo.txt expanded to _ne.foo.txt giving you unknown file errors (even though you didn't show any error messages in the output of running your script.
# 10  
Old 10-11-2012
The error I get is
Code:
Incorrect output on test 33: /home/sub3 'Unix' 'U N I X' '_king cobra.dat' '_ne$wt.foo'

# 11  
Old 10-11-2012
Quote:
Originally Posted by ramn214
The error I get is
Code:
Incorrect output on test 33: /home/sub3 'Unix' 'U N I X' '_king cobra.dat' '_ne$wt.foo'

Please show us the code that produced those error messages!
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