Hi,
I am reading a command in shell script using sed:
echo $1 | sed -e "s/^[^=]*=\(.*\)$/\1/"

what I don't understand is the sed command. I only know it tries to substitute "^[^=]*=\(.*\)$/" with "\1" and 1 is one of the positional parameters got from invoking the shell script, but have little idea about what the two things are.

Thanks for help!

Recommended Answers

All 5 Replies

echo $1 | sed -e "s/^[^=]*=\(.*\)$/\1/"

echo $1 is the argument from the shell piped to sed but it doesn't have any thing to do with \1 What's \1? The first group remembered from the original regex, and it is represented by \(some match inside\) So if I have the string "Hello World" and I pipe it to sed as sed -e 's/\(.*\) \(.*\)/Bye \2/' The result would be "Bye World". sed -e 's/\(.*\) \(.*\)/\1 there/' Would result in "Hello there"

[Edit:]
Just in case you were wondering about s/^[^=]*=\(.*\)$/ A high interpretation would be:
Erase any thing from the beginning of a line to the = sign included.

Of course, the stuff with the replacement is completely unnecessary as this

s/^[^=]*=\(.*\)$/\1/

is the same as

s/^[^=]*=//

And the second is probably, at least slightly, more effecient.

;-)

Thanks! Now I understand \1 and \(some match inside\).
But still, what doest "^[^=]*" mean? Why there are two ^s?
In the reply, what does"Erase any thing from the beginning of a line to the = sign included. " mean?

Thanks! Now I understand \1 and \(some match inside\).
But still, what doest "^[^=]*" mean? Why there are two ^s?
In the reply, what does"Erase any thing from the beginning of a line to the = sign included. " mean?

Segmented, so you can understand better.
The first ^ says match beginning of a line.
The [^=] says match any character that is not a = sign. (the ^ inside [] is different that the one for the beginning of line, inside negates the match)
The * says repeat previously as many times as possible. Since [^=] can be anything that is not a = sign, it will keep matching characters until finds a = and stop.
Put all together and you'll see that it will match any line from beginning to an equal symbol, substitute it for nothing or whatever you have after the symbol and you'll have: "Erase any thing from the beginning of a line to the = sign included. "

Can anyone explain me the below and please tell me why it is not accepting large number of strings. sed ':a;s/(([^,|]+)[|,].*)\2/\1/;ta;y/,/|/;s/||/|/g' file

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.