LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Very new to Linux/Trying to understand the Sed command (https://www.linuxquestions.org/questions/linux-newbie-8/very-new-to-linux-trying-to-understand-the-sed-command-865324/)

kayjay1 02-27-2011 11:32 AM

Very new to Linux/Trying to understand the Sed command
 
I have tried to get the desired outcome and have failed.

the blue cat was chased by the brown dog
the blue cat was not chased by the brown dog

Line should read

the blue snake was chased by the brown snake
the blue car was not chased by the brown car.

I got the first line, but the words to be substituted in the second line did not change.

sed -e 's/cat/snake/' -e 's/dog/snake/' -e '/not/s/cat/car' -e 's/dog/cat/'

I have read and found that sed does not handle long line of command, so is there some way to break this up. I tried to use pipe and \n. Very new to this.

paulsm4 02-27-2011 12:25 PM

Hi -

No, the length of the line is not the problem.
Quote:

vi test.txt
Code:

the blue cat was chased by the brown dog
the blue cat was not chased by the brown dog


Quote:

sed -e 's/cat/snake/' -e 's/dog/snake/' -e '/not/s/cat/car' -e 's/dog/cat/' test.txt
Code:

sed: -e expression #3, char 14: unterminated `s' command

Quote:

sed -e 's/cat/snake/' -e 's/dog/snake/' -e 's/dog/cat/' test.txt
Code:

the blue snake was chased by the brown snake
the blue snake was not chased by the brown snake


Quote:

sed -e 's/cat/snake/' -e 's/dog/snake/' -e 's/dog/cat/' -e 's/not chased by the brown snake/not chased by the brown car/' test.txt
Code:

the blue snake was chased by the brown snake
the blue snake was not chased by the brown car



kayjay1 02-27-2011 01:07 PM

Thank you very much. Can you also try to help me understand this. I under the the first, second, and last expression. The third substitution has me stumped. -e 's/dog/cat'
I forgot to add the file name at the end of my line. I apologize for that. Thank you again.


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