Printing the last column of a line in a file

awk '( print second last column)
awk last column
awk print last column minus 1
awk '(print last column separated by comma)
awk '(print last column minus 1)
awk print column
awk print all but last field
awk '(print last row)

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?

You don't see anything, because of buffering. The output is shown, when there are enough lines or end of file is reached. tail -f means wait for more input, but there are no more lines in file and so the pipe to grep is never closed.

If you omit -f from tail the output is shown immediately:

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

@EdMorton is right of course. Awk can search for A1 as well, which shortens the command line to

tail file | awk '/A1/ {print $NF}'

or without tail, showing the last column of all lines containing A1

awk '/A1/ {print $NF}' file

Thanks to @MitchellTracy's comment, tail might miss the record containing A1 and thus you get no output at all. This may be solved by switching tail and awk, searching first through the file and only then show the last line:

awk '/A1/ {print $NF}' file | tail -n1

Print second last column/field in awk, If you want the last field only and not both first and last, you could try: rev file | cut -​f 1. The rev reverses each line of the file and the cut prints  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.

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

awk '{print $(NF)}' 

Print last element of each row, Originally Answered: how can I use awk to print the last column of each line in a file? You're looking for $NF. For example: Given the following file: $ cat filename The tail is the most common command used. tail command prints the last part of the files. -1 specifies to print one line from the last part. $ tail -1 file Linux 2. The END label in awk makes it even more easily. END label is reached once the entire file is parsed.

You can do this without awk with just some pipes.

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

Unix: How can I use awk to print the last column of each line in a file , How to print last column of line. Original Post by learnbash. learnbash. Hello folks​, Please guide me i have a file file.txt that have below text. PETER JOHN  Printing the nth word or column in a file or line We may have a file having a number of columns, and only a few will actually be useful. For … - Selection from Linux Shell Scripting Cookbook - Second Edition [Book]

One way using awk:

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

How to print last column of line, Outside the loop we print "" to print the new line character between input rows. Examples: If we want to print the last two columns of the ls -l  awk column printing examples. Suppose you have a file named foo with these contents, three columns of data separated by blanks: $ cat foo 1 2 3 a b c Next, here are the awk examples: $ awk '{ print $1 }' foo 1 a $ awk '{ print $2 }' foo 2 b $ awk '{ print $3 }' foo 3 c $ awk '{ print $1, $3 }' foo 1 3 a c $ awk '{ print $3, $1 }' foo 3 1 c a

maybe this works?

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

Use awk to print the last N columns of a file or a pipe – Bytefreaks.net, That will be the last field -4, the last field -3, until the actual last field. Reading from a file with this content (file.txt): f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 c1 c2  I want to print the column number of last character of line. I have a file. So I want to print the column number of last character of line which has highest number in file. Thanks.

command line - How do I print the last 5 fields in awk?, Hi,. If you want to write a shell script you can read every line and print last field of line otherwise it is better to use "awk". The content of NF is number of fields and  Run awk on the file as described below. The line with the $ sign is the command. The last two lines are the output. $ awk '{print $(NF-4)" "$(NF-3)" "$(NF-2)" "$(NF-1)" "$NF}' file.txt f6 f7 f8 f9 f10 c6 c7 c8 c9 c10 Note: As pointed out by Cyrus in the comment, I removed the bash script and left just the print statement to make it simpler and faster.

Cut Last Column from a File Unix, How to use 'awk' to print columns from a text file (in any order). By Alvin Alexander. Last updated: January 6, 2020. One of my favorite ways to This built​-in ability to process every line in the input file is a great feature of awk . While all of these  Assuming you have a arbitary text file “file.txt” like this [code]This is first line Another line This is last line [/code]A python3 program to read last 2 lines of a given text file will be [code]def read_last_lines(filename, no_of_lines=1): fil

How to use 'awk' to print columns from a text file (in any order , the last field of an input line. The variable $0 (zero) holds the value of the entire line. This command printed the fifth column of a long file listing, which contains the file size, and the last column, the name of the file. This output is not very 

Comments
  • You never need grep+awk since awk can do it's own RE comparison. "grep A1 | awk '{print $NF}'" should just be "awk '/A1/{print $NF}'". Also, if you aren't using tail -f then you don't need tail at all, yoiu can just do: awk '/A1/{f=$NF} END{print f}' file
  • This isn't quite right, as the column may not appear in the window that raw tail gives you. Unless you know it is going to appear with a certain frequency, it would be safer to awk '/A1/ {print $NF}' file | tail -n1.
  • This solves your problem, but it doesn't really use awk. Hope it's good enough for you anyway.
  • I thought that was the right solution to the posted problem (+1!) but now I see I don't understand the question. The OP wants the last field from the LAST LINE of the file, but there is no LAST LINE if you're using tail -f. Maybe he means the last line CURRENTLY in the file, in which case see one of my other comments.
  • Very good and simple. Thumbs up. Thumbs down for awk's cryptic syntax :)
  • You don't need split() and an array, just save the last field and print that: awk '/A1/{f=$NF} END{print f}' file
  • Looks too complicated, just do perl -lane 'print $F[2] if /A1/' rayne.txt
  • @Hi-Angel..the question asks for last occurrence of A1. your answer will print all matches.. btw if you like my answer, pls upvote it
  • How is this even related to the OP's question?