LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-23-2008, 09:55 AM   #1
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Rep: Reputation: 32
sed substituting string


Hi,

I have a following text file called my.config:

Code:
Section "General"
        TYPE = $TYPE
        PATH =
EndSection
How can I use sed to directly change the last parameter (string) on each line (specifically).

I have managed (tried) to this so far:

sed '/General/,/EndSection/{n; /TYPE/s/????/hej/}' myconfig

But I just can't get it to work. Please not that "????" is not actually what I put in bash. I just don't know what to put there and also it only works for the first line in the section.

The result I want is:


Code:
Section "General"
        TYPE = ALL
        PATH = /tmp/
EndSection
OR

Code:
Section "General"
        TYPE = Generic
        PATH =
EndSection
Any idea?

Thanks.
Indy
 
Old 12-23-2008, 10:31 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Would this help:

sed -e '/General/,/EndSection/s/\$TYPE/Generic/' -e '/General/,/EndSection/s/\(PATH =\)/\1\/tmp\//' infile

First part (after the first -e) looks for and replaces $TYPE with Generic, the second part looks for PATH = and replaces it with PATH = /tmp/. All this is done only if it is between General and EndSection

Hope this helps.
 
Old 12-23-2008, 10:35 AM   #3
cmnorton
Member
 
Registered: Feb 2005
Distribution: Ubuntu, CentOS
Posts: 585

Rep: Reputation: 35
Without knowing a little more about this problem, I am not sure sed is the ideal choice for substitution in this case. Treat each line uniquely as to how you want to substitute it. I am not sure why you are including General in your substitution.

I put these strings in a file -- test.sed -- and ran it

{
s/TYPE = \$TYPE/TYPE = ALL/g
s/PATH =.*/PATH = \/tmp\//g
}

sed -f test.sed test.txt, where test.txt contained your text to be substituted.

There are probably many more sophisticated ways to handle it.
 
Old 12-24-2008, 05:05 AM   #4
freeindy
Member
 
Registered: Nov 2002
Posts: 207

Original Poster
Rep: Reputation: 32
Thanks lads. Both should work. I see the error I made.

Actually, the simplest is to replace the whole thing. And more clean as well. Thanks nmnorton. I like the solution. I don't understand why I didn't think of this.

I will use it like this.

{
s/TYPE =.*/TYPE = ALL/g
s/PATH =.*/PATH = \/tmp\//g
}

cheers.

Indy
 
  


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
i am missing new lines when substituting with sed in a bash script FIRATYILDIRIM Programming 7 12-15-2008 03:07 PM
Trying to change String using sed with a string \/home\/user\/Desktop icecoolcorey Programming 10 06-12-2008 11:32 PM
substituting multiline string in sed rohit2983 Programming 3 01-31-2007 09:00 AM
Remove string in sed twantrd Programming 7 09-13-2006 02:28 PM
need help substituting with sed parker Programming 2 08-08-2003 10:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:36 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration