7

I want to extract last word of a file say a.txt in bash/sed/awk.

How can I do it?

9 Answers 9

21

To get to get the last word in the last line:

awk 'END {print $NF}' file 
Sign up to request clarification or add additional context in comments.

Comments

6

Updated.

If you want to use awk and make sure there is a word, use this:

tac a.txt | awk 'NF{print $NF; exit}' 

tac prints the file in reverse. NF in front of the {} block makes it work whenever the line is not empty. In such case, it prints the last field (NF stands for number of fields, so $NF is the last one), and then exits.

Test

$ cat a hello my name is blabla and this is my comment. <--- note an empty line $ tac a | awk 'NF{print $NF; exit}' comment. 

Or also, as suggested by Kent:

awk '{w=NF?$NF:w} END{print w}' file 

w=$NF?$NF:w. This is a ternary operator: if NF>0 (no empty line), set w to the last field. Otherwise, keep it the way it was. Finally, in END{}, print the last saved word.


In case you want to make it with sed, you can use this, that works in case there is no empty lines at the end:

sed -n '${s/.* //; p}' a.txt 

Explanation

  • $ stands for last line of the file. In that case, do what is inside {}.
  • s/.* //; p remove everything up to last space. Then print p.

7 Comments

I guess he wants something like awk 'END{print $NF}' file I said like because, the last line of the file could be empty. since you came first, you could do some fix and complete the answer.
Errr true, I did understand it wrongly. Jotne posted his answer now, so I will just remove mine.
well as I commented, awk 'END{print $NF}' file won't always work.
+1 since OP said no empty line, it is much easier. , I just want to comment awk '{w=$NF?$NF:w}END{print w}' file
s/.* //; p is fine unless line end with a space. mya i suggests/.* \([[:space:]]\{1,\}[[:space:]]*$/\1/;p so event with line with 1 word (or not, but this depend on exact resquest) on last line it works
|
1

Try this awk command also,

awk -v RS="\0" '{print $NF}' file 

RS="\0" turns all the records in a file to a single single record. And then {print $NF} prints the last field of that single record.

Comments

1

You can also use sed command ,

$sed -nr '${s/.* (.*)$/\1/pg}' File_name 

1 Comment

you should put the p ouside the s/// or it will fail if there is not at least 2 word on last line and g is not needed in this case, there is only 1 end per line. Finaly, it failed on line with last position is a space character.
1

Using tail and grep :

tail -1 myFile.txt | grep -oE '[^ ]+$' 

Comments

0

You can also get the last word with grep and tail:

<a.txt grep -o '\w\+' | tail -n1 

Comments

0

sed variant to support empty last line (if any):

sed -n '/[^ ]/h; ${g;s/ *$//; s/.* //p}' a.txt 

Comments

0

This might work for you (GNU sed):

sed -r '/\w/{s/.*\W(\w+)\W*$/\1/;h};$!d;x;/./!d' file 

Saves the last word of the current line in the hold space then deletes the line. At the end of the file it retrieves the last word and prints it out unless there was no word in which case it deletes the empty line.

An alternative way is to slurp the whole file into memory:

sed -r ':a;$!{N;ba};s/.*\W(\w+)\W*$/\1/p;d' file 

Comments

0
 sed -n '/^$/!{$ s/.* \(\w*\)$/\1/p}' 

This will omit blank lines and print the last word on the last non-blank line.

  • /^$/! find non-blank lines
  • $ s/ on the last line substitute
  • .* (\w*)$ capture anything between the ()
  • \1 substitute line for capture group ()
  • p print it

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.