-1

How to get the list of filenames which have last line (or line on position X) matching certain pattern?

We are looking for oneliner.

1
  • We are not a script writing service. We expect users to tell us what they have tried so far (including any scripts they are using) and where they're stuck so that we can help with specific problems. Questions that only ask for scripts are too broad and are likely to be put on hold or closed. Please read How do I ask a good question?.
    – DavidPostill
    Feb 7, 2016 at 18:31

1 Answer 1

1
awk '/pattern/ {last_matching_line = FNR} (FNR != NR && FNR==1) {if (last_FNR == last_matching_line) {print last_filename}} {last_FNR = FNR; last_filename = FILENAME} END {if (FNR == last_matching_line) {print FILENAME}}' file1 file2...
  • First save the last_matching_line by FNR.
  • Then check if it is the last line by checking if awk is processing another file (FNR==1) or is processing the last file (END).
  • In either of these two cases, print the FILENAME.

For the line on position X question, it is easier because there is only one case to check: FNR == X.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .