LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed command doubt (https://www.linuxquestions.org/questions/linux-newbie-8/sed-command-doubt-4175427960/)

sujithspillai90 09-19-2012 04:42 AM

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.

Snark1994 09-19-2012 04:48 AM

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.

sujithspillai90 09-19-2012 04:57 AM

Many thanks :)

Mauritius 09-19-2012 05:25 AM

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.

sujithspillai90 09-20-2012 03:55 AM

First command worked for me.But thank you for the detailed explanation.

Snark1994 09-20-2012 04:51 AM

Quote:

Originally Posted by Mauritius (Post 4784022)
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.

Snark1994 09-21-2012 05:32 AM

Quote:

Originally Posted by sujithspillai90 (Post 4784886)
First command worked for me.But thank you for the detailed explanation.

Could you mark this thread as 'SOLVED', then, please? Thanks,

mreff555 09-21-2012 08:25 AM

Quote:

Originally Posted by Mauritius (Post 4784022)
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.


All times are GMT -5. The time now is 07:46 PM.