LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 09-19-2012, 04:42 AM   #1
sujithspillai90
Member
 
Registered: Jun 2011
Location: India
Posts: 30

Rep: Reputation: Disabled
Question sed command doubt


Hi ,

I want to replace "hello test" with "Hello this is test" in a file called "TODAYSTST".But there are many other lines in the same file which is having "hello test".But I would like to replace only the first occurence"hello test" using sed.

Please could anyone help me in this using sed command options.
 
Old 09-19-2012, 04:48 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Well, from some searching and experimentation, this seems to work:

Code:
sed '0,/hello test/s//Hello this is test/' TODAYSTST
though I haven't yet worked out why it works... The '0,/hello test/' means it's only going to apply the substitution up to the first occurence of 'hello test', and my guess would be that the pattern then defaults to 'hello test' for some reason in the substitution.

Last edited by Snark1994; 09-19-2012 at 04:53 AM.
 
Old 09-19-2012, 04:57 AM   #3
sujithspillai90
Member
 
Registered: Jun 2011
Location: India
Posts: 30

Original Poster
Rep: Reputation: Disabled
Many thanks
 
Old 09-19-2012, 05:25 AM   #4
Mauritius
LQ Newbie
 
Registered: Oct 2004
Posts: 2

Rep: Reputation: 0
Code:
sed '0,/hello test/s//Hello this is test/' TODAYSTST
I think this is not complete correct. Better would be

Code:
sed '0,/.*hello test/s/hello test/Hello this is test/' TODAYSTST
0,/.*hello test/ is the Lineadress for the substitution. It means from the first line to the fist line which contains "hello test" at least one time.
s/hello test/Hello this is test/ substitute then in that Linegroup the text "hello test" with "Hello this is test" one time.
 
Old 09-20-2012, 03:55 AM   #5
sujithspillai90
Member
 
Registered: Jun 2011
Location: India
Posts: 30

Original Poster
Rep: Reputation: Disabled
First command worked for me.But thank you for the detailed explanation.
 
Old 09-20-2012, 04:51 AM   #6
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Quote:
Originally Posted by Mauritius View Post
Code:
sed '0,/hello test/s//Hello this is test/' TODAYSTST
I think this is not complete correct. Better would be

Code:
sed '0,/.*hello test/s/hello test/Hello this is test/' TODAYSTST
0,/.*hello test/ is the Lineadress for the substitution. It means from the first line to the fist line which contains "hello test" at least one time.
s/hello test/Hello this is test/ substitute then in that Linegroup the text "hello test" with "Hello this is test" one time.
Have you actually tried this?

Using the file
Quote:
First line
second hello test line
third line hello test
hello test fourth line
my example works perfectly. You certainly don't need the '.*' (because it already matches anywhere on the line), and although you're correct that we are performing 's/hello test/Hello this is test/', it seems to default to the string we searched for.
 
Old 09-21-2012, 05:32 AM   #7
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Quote:
Originally Posted by sujithspillai90 View Post
First command worked for me.But thank you for the detailed explanation.
Could you mark this thread as 'SOLVED', then, please? Thanks,
 
Old 09-21-2012, 08:25 AM   #8
mreff555
Member
 
Registered: Sep 2011
Location: Philly
Distribution: Gentoo
Posts: 444

Rep: Reputation: Disabled
Quote:
Originally Posted by Mauritius View Post
Code:
sed '0,/hello test/s//Hello this is test/' TODAYSTST
I think this is not complete correct. Better would be

Code:
sed '0,/.*hello test/s/hello test/Hello this is test/' TODAYSTST
0,/.*hello test/ is the Lineadress for the substitution. It means from the first line to the fist line which contains "hello test" at least one time.
s/hello test/Hello this is test/ substitute then in that Linegroup the text "hello test" with "Hello this is test" one time.
The first command is correct. It is just shorthand for the second.

when the arguement you are searching for is the same as the argument you want to replace you can omit the second instance of it.
 
  


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
[SOLVED] sed gives :sed: -e expression #1, char 1: unknown command: `'' samasat Linux - Newbie 10 06-09-2012 05:31 PM
[SOLVED] sed help to run sed command against multiple different file names bkone Programming 2 04-16-2012 12:27 PM
[SOLVED] sed doubt divyashree Programming 3 12-26-2010 07:59 AM
ps command - doubt Uday123 Linux - General 1 09-10-2009 06:47 PM
os command doubt===urgent google_man Solaris / OpenSolaris 10 08-04-2003 09:35 AM


All times are GMT -5. The time now is 02:06 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