replacing strings in many files using tcsh
Hi all,
I am using a tcsh script to execute commands and call other programs. At one point I need to replace string1 with string2. I have posted here before and kind people referred me to sed which partially worked. My problem is that the strings replacement is executed within a 'foreach' command. Here is that part in the script: #!/usr/bin/tcsh foreach number (1 2 3 4 5 6 7 8 9 10) cd ${number} sed 's/string1/${number}/' file1>file2 cd ../ end which is supposed to: 1. enter the directory named '1' 2. replace string1 with the number '1' in file1 3. save the new file as file2 and exit the directory 4. recursively repeat until the 10th directory the problem is that instead of replacing string1 with the number, it types in the string '${number}' without recognizing that i am calling the value (e.g 1,2...). Does anyone know how to fix this - replace string1 with the actual number? I saw this past posting (http://www.linuxquestions.org/questi...d+foreach+tcsh) but it seems slightly different and i am not sure how to apply on my it problem. I am relatively new to to the unix world so please take that into account if you are writing back. any suggestion is highly appreciated - thanks a lot, Ben |
The reason that ${number} was left that way was the expression is wrapped in single quotes. Have you tried it this way?
Code:
sed "s/string1/${number}/" file1>file2 |
FYI,
cd .. is sufficient (the '/' is redundant there) |
it worked! one additional short question
Thanks a lot for correcting my syntax. I have a short additional question - how can I save the modified file under the same name, without having to create file2? is it possible with sed? (I tried to type file1>file1, but the result is in an empty file1)
many thanks, Ben Quote:
|
Quote:
|
If your version of sed supports the -i switch it is possible. Make sure you test first with a temporary file (or backup your original before starting) though.
|
All times are GMT -5. The time now is 06:28 AM. |