Print last matching line?

后端 未结 5 773
無奈伤痛
無奈伤痛 2021-01-14 08:20

Using awk, I would like to print the last matching line of a file. I would like only the matching line itself, not any range of lines. I can use a command like

5条回答
  •  情话喂你
    2021-01-14 08:43

    You can assign values to variables and print them at the end of processing both the lines and their number.

    File:

    $ cat file
    foo1
    foo2
    foo3
    4
    5
    

    Command:

    $ awk '/foo/ {a=$0;n=NR} END{print n, a}' file
    3 foo3
    

提交回复
热议问题