LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed error (https://www.linuxquestions.org/questions/linux-newbie-8/sed-error-4175434571/)

vicky007aggrwal 10-29-2012 02:39 AM

sed error
 
Can somebody please suggest why i am getting below error in sed command while doing the basic substitution

echo "sunday"|sed 's/sunday/night'
sed: -e expression #1, char 14: unterminated `s' command

sycamorex 10-29-2012 03:04 AM

You need to terminate the s command with the closing /

vicky007aggrwal 10-29-2012 03:21 AM

Thanks

vicky007aggrwal 10-29-2012 03:23 AM

One more thing , can you suggest or give an example which will help me in understand that when to use "-e" option in sed

eg:- sed -e "s/ddd/ee/g"

i am not able to establish the situation in which i can use this... pls help

grail 10-29-2012 04:53 AM

Do 2 or more changes to the same line.

druuna 10-29-2012 04:56 AM

With sed's -e option you can string together multiple sed action's.

Consider this:
Code:

echo -e "Monday\nTuesday" | sed -e 's/Mon/Sun/' -e '/Tuesday/d'
Sunday

The above changes Mon to Sun (blue part) and also looks for lines that contain Tuesday and deletes them (green part). You cannot do this in one action, two are needed. You need the -e option for this (or use 2 seperate sed statements, which is not always possible).

There is one thing that you need to be aware of; Changes made in the first action are noticed in the second (third, ... ) action. Have a look at this simple silly example:
Code:

echo "sunday" | sed -e 's/sunday/night/' -e 's/night/week/'
week

The first action looks for sunday and changes this into night, the second action looks for night and changes this into week.

Have a look at this link as well: Sed - An Introduction and Tutorial

David the H. 10-30-2012 11:46 AM

To be more exact, sed takes one line at a time into its 'pattern buffer' and attempts to apply all of the given expressions to it, in the order given. Then moves on to the next line and does the same thing.

To restrict an action to only specific lines, you usually use addressses in front of the expressions. You can also create nested expressions in '{}' brackets for complex actions. Multi-line work can get even more complex than that.

BTW, in most seds you can usually use a semicolon to separate expressions instead of using individual -e options.

Code:

$ echo -e "sunday morning\nmonday morning" | sed '/sunday/ s/morning/night/ ; /monday/ s/morning/afternoon/'
sunday night
monday afternoon

Here are a few useful sed references:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

vicky007aggrwal 10-31-2012 01:24 PM

Thanks all for helping out


All times are GMT -5. The time now is 09:10 AM.