LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   replacing strings in many files using tcsh (https://www.linuxquestions.org/questions/linux-software-2/replacing-strings-in-many-files-using-tcsh-625238/)

mcbenus 03-02-2008 06:40 PM

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

gilead 03-02-2008 07:42 PM

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

chrism01 03-02-2008 11:26 PM

FYI,
cd ..
is sufficient (the '/' is redundant there)

mcbenus 03-03-2008 02:43 PM

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:

Originally Posted by gilead (Post 3076171)
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


normscherer 03-03-2008 05:00 PM

Quote:

Originally Posted by mcbenus (Post 3076946)
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

The short answer is you can't. You have to xx>file2;mv -f file2 file1

gilead 03-03-2008 05:50 PM

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.