Bash, text file removes all text on each line up to the last space
AWK
Print the last field of each line with awk...
The last field is indexed using a variable NF
that contains the number of fields for each row. We index it with a dollar sign, the resulting one-liner is simple.
awk '{ print $NF }' file
rs, cat and tail
Another way is to transfer the contents of the file, then grab the last line and transpose again (this is pretty easy to see).
Received pipe:
cat file | rs -T | tail -n1 | rs -T
cut and rev
Using cutand rev
we could also achieve this goal by changing direction, cutting off the first field, and then changing it again.
rev file | cut -d ' ' -f1 | rev
SED
Using sed we will just remove all characters until a space is found with regex ^.* [^ ]*$
... This regular expression means the start of a line ^
, followed by any sequence of characters .*
and a space
. The rest is a sequence of non-whitespace [^ ]*
until the end of the line $
. Saddle one-liner:
sed 's/^.* \([^ ]*\)$/\1/' file
Where we strip the last part (between \(
and \)
) and put it back for the whole string. \1
means the first found group, which is the last field.
Notes
-
As Ed Norton aptly pointed out, we just can't catch the group and remove the old part of the regex. This can be done as easily as
sed 's/.* //' file
Which is surprisingly less complicated and elegant. -
For more information see
man sed
andman awk
.
source to share