Tuesday, September 27, 2011

Multiple sed expression in one line

To do multiple sed expression in one line, you can use -e a few times according to your need. For example:

To subtitute 'little' with 'big' and 'lamb' with 'cow' in "Mary had a little lamb" we can use:

$ echo 'Mary had a little lamb' | sed -e 's/little/big/' -e 's/lamb/cow/'
Mary had a big cow
where -e is for expression flag, 's/little/big/' is the first expression and 's/lamb/cow/' is the second expression

That's all.


Anonymous said...

between sed and awk which one is easier to use?

Need your advise. Tq

blackorga said...

Hi Anonymous,

In my opinion, sed is easier to understand because it follows some command that being used in vi, like the 's' for subtitute