[file] Printing the last column of a line in a file

I have a file that is constantly being written to/updated. I want to find the last line containing a particular word, then print the last column of that line.

The file looks something like this. More A1/B1/C1 lines will be appended to it over time.

A1 123 456
B1 234 567
C1 345 678
A1 098 766
B1 987 6545
C1 876 5434

I tried to use

tail -f file | grep A1 | awk '{print $NF}'

to print the value 766, but nothing is output.

Is there a way to do this?

This question is related to file awk tail

The answer is


One way using awk:

tail -f file.txt | awk '/A1/ { print $NF }'

To print the last column of a line just use $(NF):

awk '{print $(NF)}' 

ls -l | awk '{print $9}' | tail -n1


Using Perl

$ cat rayne.txt
A1 123 456
B1 234 567
C1 345 678
A1 098 766
B1 987 6545
C1 876 5434


$ perl -lane ' /A1/ and $x=$F[2] ; END { print "$x" } ' rayne.txt
766

$

You can do this without awk with just some pipes.

tac file | grep -m1 A1 | rev | cut -d' ' -f1 | rev

Execute this on the file:

awk 'ORS=NR%3?" ":"\n"' filename

and you'll get what you're looking for.


awk -F " " '($1=="A1") {print $NF}' FILE | tail -n 1

Use awk with field separator -F set to a space " ".

Use the pattern $1=="A1" and action {print $NF}, this will print the last field in every record where the first field is "A1". Pipe the result into tail and use the -n 1 option to only show the last line.


You can do all of it in awk:

<file awk '$1 ~ /A1/ {m=$NF} END {print m}'

maybe this works?

grep A1 file | tail -1 | awk '{print $NF}'

Not the actual issue here, but might help some one: I was doing awk "{print $NF}", note the wrong quotes. Should be awk '{print $NF}', so that the shell doesn't expand $NF.


Examples related to file

Gradle - Move a folder from ABC to XYZ Difference between opening a file in binary vs text Angular: How to download a file from HttpClient? Python error message io.UnsupportedOperation: not readable java.io.FileNotFoundException: class path resource cannot be opened because it does not exist Writing JSON object to a JSON file with fs.writeFileSync How to read/write files in .Net Core? How to write to a CSV line by line? Writing a dictionary to a text file? What are the pros and cons of parquet format compared to other formats?

Examples related to awk

What are NR and FNR and what does "NR==FNR" imply? awk - concatenate two string variable and assign to a third Printing column separated by comma using Awk command line Insert multiple lines into a file after specified pattern using shell script cut or awk command to print first field of first row How to run an awk commands in Windows? Linux bash script to extract IP address Print line numbers starting at zero using awk Trim leading and trailing spaces from a string in awk Use awk to find average of a column

Examples related to tail

Printing the last column of a line in a file How can I tail a log file in Python? Head and tail in one line How to 'grep' a continuous stream? Unix tail equivalent command in Windows Powershell A Windows equivalent of the Unix tail command Get last n lines of a file, similar to tail