Bash, text file removes all text on each line up to the last space

I have a file with this format:

First Last UID
First Middle Last UID

      

Basically, some names have middle names (and sometimes more than one middle name). I just want the file to be as UID only.

Can a sed or awk command be executed that removes everything up to the last space?

+3


source to share


2 answers


AWK

Print the last field of each line with ...

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 and 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 we will just remove all characters until a space is found with ^.* [^ ]*$

... 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

    and man awk

    .

+6


source


Usage grep

:

$ grep -o '[^[:blank:]]*$'  file
UID
UID

      



-o

tells grep to print only the relevant part. The regular expression [^[:blank:]]*$

matches the last word in the string.

+1


source







All Articles