Printing the last column of a line in a file

前端 未结 11 1441
遇见更好的自我
遇见更好的自我 2020-12-12 11:33

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 fi

相关标签:
11条回答
  • 2020-12-12 12:19

    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
    
    $
    
    0 讨论(0)
  • 2020-12-12 12:21

    maybe this works?

    grep A1 file | tail -1 | awk '{print $NF}'
    
    0 讨论(0)
  • 2020-12-12 12:22

    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.

    0 讨论(0)
  • 2020-12-12 12:24

    You can do all of it in awk:

    <file awk '$1 ~ /A1/ {m=$NF} END {print m}'
    
    0 讨论(0)
  • 2020-12-12 12:25

    One way using awk:

    tail -f file.txt | awk '/A1/ { print $NF }'
    
    0 讨论(0)
  • 2020-12-12 12:26

    You can do this without awk with just some pipes.

    tac file | grep -m1 A1 | rev | cut -d' ' -f1 | rev
    
    0 讨论(0)
提交回复
热议问题