LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 03-02-2008, 06:40 PM   #1
mcbenus
LQ Newbie
 
Registered: Feb 2007
Posts: 24

Rep: Reputation: 15
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
 
Old 03-02-2008, 07:42 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 151Reputation: 151
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
 
Old 03-02-2008, 11:26 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
FYI,
cd ..
is sufficient (the '/' is redundant there)
 
Old 03-03-2008, 02:43 PM   #4
mcbenus
LQ Newbie
 
Registered: Feb 2007
Posts: 24

Original Poster
Rep: Reputation: 15
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 View Post
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
 
Old 03-03-2008, 05:00 PM   #5
normscherer
Member
 
Registered: Sep 2005
Location: On the road
Distribution: Ubuntu 8.10
Posts: 40

Rep: Reputation: 15
Quote:
Originally Posted by mcbenus View Post
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
 
Old 03-03-2008, 05:50 PM   #6
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 151Reputation: 151
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
getting strings for all the files and folders in a folder Hard_Working_ Programming 6 04-07-2007 10:55 AM
Help with sed - replacing strings thulley Linux - Software 4 08-22-2006 10:07 AM
howto allow strings in header (.h) files? RHLinuxGUY Programming 1 05-20-2006 09:07 PM
group files by strings in dir Melsync Programming 7 12-17-2005 12:53 PM
Searching files for strings tmoorman Linux - Software 4 01-08-2004 01:46 PM


All times are GMT -5. The time now is 06:22 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration