1825795 Members
2203 Online
109687 Solutions
New Discussion

sed problems with "*"

 
Hermann Merz_1
Advisor

sed problems with "*"

Hello,
i?am a little bit confused about sed.

How can i cut away for example all the lines before the last ":" in one line.

For my Opionion, the following command should have as result: "Status READY" but sed does change no line ???

echo "ab:cd ef: READY" | sed s/ab*ef:/Status/g

Any ideas ???
Update the world
9 REPLIES 9
Joseph C. Denman
Honored Contributor

Re: sed problems with "*"

In using sed, the * is simply that, a *. It is not used as a wildcard character.

If I had only read the instructions first??
James R. Ferguson
Acclaimed Contributor

Re: sed problems with "*"

Hermann:

# echo "ab:cd ef: READY"|sed s/"ab:cd ef:"/Status/g

...will give the desired results.

...JRF...
harry d brown jr
Honored Contributor

Re: sed problems with "*"

You probably want something like this:

echo "ab:cd ef: READY" | sed 's/\(.*:\) .*$/\1Status/'



Live Free or Die
harry d brown jr
Honored Contributor

Re: sed problems with "*"

Try this instead:

echo "ab:cd ef: READY" | sed 's/\(.*:\)\(:.*$\)/\1: Status/'

The string "\(.*:)" will match as much of the input string as possible. The result will be placed into "\1"

The string "\(:.*$\)" will match the end of the sting ($) back to the last ":"

Live Free or Die
harry d brown jr
Honored Contributor

Re: sed problems with "*"

SORRY, I pasted it wrong, try this instead:

echo "ab:cd ef: READY" | sed 's/\(.*\)\(:.*$\)/\1: Status/'

The string "\(.*\)" will match as much of the input string as possible. The result will be placed into "\1"

The string "\(:.*$\)" will match the end of the sting ($) back to the last ":"
Live Free or Die
Robin Wakefield
Honored Contributor

Re: sed problems with "*"

Hermann,

In your specific example, you need a "." before the "*" to match any number of characters before "ef", i.e.

# echo "ab:cd ef: READY" | sed s/ab.*ef:/Status/g
Status READY

Rgds, Robin.
Wodisch
Honored Contributor

Re: sed problems with "*"

Hello Herrmann,

this seems to be much easier with "awk":

echo "ab:cd ef: READY" | awk -F: '{print $NR;}'

The option "-F" sets the delimiter, and the
value of "$NR" (number of records) happens to
the number of the last field - which is the
rest of line after the last ":", of course ;-)

HTH,
Wodisch
harry d brown jr
Honored Contributor

Re: sed problems with "*"

Hermann,

First I want to thank you for posting this question, because its been a great learning experience. And Robin, thanks, because I blew the question, I didn't see that he was trying to replace the string before the word "READY" with the word "Status".

This will work with all "status's":

echo "ab:cd ef: READY" | sed 's/\(.*:\)\(.*$\)/Status\2/'
Status READY

or

echo "ab: cd:ab ef: ABORTED" | sed 's/\(.*:\)\(.*$\)/Status\2/'
Status ABORTED
Live Free or Die
Leslie Chaim
Regular Advisor

Re: sed problems with "*"

Hermann,

First of all, as stated by Robin the '.' is needed before the '*'. The '*' matches and char is only valid when working with the shell and FNG (file name generation). In sed you work with regular expressions. And in regular expression the '*' is a quantifier for whatever it PRECEDES. So the '.' in '.*' says match one and only one char, and the '*' says any number of times.

Secondly, there is an issue of greediness. The '.*' will actually go to the end of line, therefore in you particular case you can simply
say:

echo "ab: cd:ab ef: ABORTED" |sed 's/.*:/Status/'

1) The .* goes to the end of line because the quantifiers are greedy.
2) The : requires a match
3) The regex engine BACKTRACKS until that point
4) Leaving you left what ever is after the LAST :
5) And ... the replacement part is quite simple now:)

Hope this helps.

Leslie
If life serves you lemons, make lemonade