Hallo,
i want to keep only the last word from a line so i've written this:
Code:
echo 'aaa' | sed 's/.* \([^ ]*$\)/\1/'
echo 'aaa bbb' | sed 's/.* \([^ ]*$\)/\1/'
echo 'aaa bbb ccc' | sed 's/.* \([^ ]*$\)/\1/'
echo 'aaa bbb ccc ddd' | sed 's/.* \([^ ]*$\)/\1/'
echo 'aaa bbb ccc ddd eee' | sed 's/.* \([^ ]*$\)/\1/'
Code:
aaa
bbb
ccc
ddd
eee
Works properly so far but its not quite clear why. The intention was to match the whole line and output just a selected part (marked with () and \1).
My first attempt was rather this (it has no space after .*) and it didn't work (empty output):
Code:
echo 'aaa bbb ccc' | sed 's/.*\([^ ]*$\)/\1/'
So, what does this additional space actually do? Is it a separator in a regular expression? Will the second match '[^ ]*$' (if its a second match at all) inherit the result from the first one '.*'? It doesn't look like its treated literally.
Any idea?
Edit:
Tested on GNU sed (cygwin).