How to Get the Last Word in Each Line with Bash

How do I get the last word in each line with bash

Try

$ awk 'NF>1{print $NF}' file
example.
line.
file.

To get the result in one line as in your example, try:

{
sub(/\./, ",", $NF)
str = str$NF
}
END { print str }

output:

$ awk -f script.awk file
example, line, file,

Pure bash:

$ while read line; do [ -z "$line" ] && continue ;echo ${line##* }; done < file
example.
line.
file.

extract last word from each line in shell

I usually use rev | cut | rev to extract the columns from the right.

 rev result.txt | cut -d. -f1 | rev

rev command in Linux is used to reverse the lines characterwise.

cut command in UNIX is for cutting out the sections from each line of files and writing the result to standard output.

  • -d denotes the delimiter (here .)
  • -f says which field(s) to extract (here the 1st one)

Use cut in shell to extract last word

cut can't count from the right. But you can use rev to reverse each line, than count from the left normally, and revert the line back. It's still surprisingly fast.

rev foo.txt | cut -d' ' -f1 | rev
  • -d specifies the delimiter, I guess you want spaces when counting words
  • -f specifies which field(s) to extract. Use -c to extract individual characters.

Extract last word of a file in bash/sed/awk

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

awk 'END {print $NF}' file

How to extract last part of string in bash?

How do you know where the value begins? If it's always the 5th and 6th words, you could use e.g.:

B=$(echo "$A" | cut -d ' ' -f 5-)

This uses the cut command to slice out part of the line, using a simple space as the word delimiter.

Bash: Store the last word of each line starting with

Yes, this should do it:

tail -f growing.dat | awk '/15 RT/ {print $NF}'

tail -f is very efficient, as it listens for file modify event and only outputs new lines when added (no need to loop and constantly check if file was modified). awk script will simply output the last field for each line that contains 15 RT.

Edit. Additionally, if you wish to store that output to a file, and monitor the values in terminal, you can use tee:

tail -f growing.dat | awk '/15 RT/ {print $NF}' | tee values.log

Since awk is buffering output, to see the values in real-time, you can flush the output after each update:

tail -f growing.dat | awk '/15 RT/ {print $NF; fflush()}' | tee values.log

Edit 2. If the file doesn't exist initially, you should use tail -F:

tail -F growing.dat | awk '/15 RT/ {print $NF}'

that way, tail will keep retrying to open file if it is inaccessible, it looks like this (message is printed to stderr):

tail: cannot open 'growing.dat' for reading: No such file or directory
tail: 'growing.dat' has appeared; following new file
-5.1583E+04

Get and process the last word of each line while looping through lines of text

With awk:

awk '$NF<1000' file.txt

$NF is the value of the last field and $NF<1000 checks if the value is less than 1000, if so then the line is printed.

To save the output in another file e.g. out.txt:

awk '$NF<1000' file.txt >out.txt

Example:

% cat file.txt 
apples asd 45 321 7000
oranges gl 78 102 850
some ltd 83 15 500
other nova 80 50 3500
stuff 600 65 115 450

% awk '$NF<1000' file.txt
oranges gl 78 102 850
some ltd 83 15 500
stuff 600 65 115 450


Related Topics



Leave a reply



Submit