0

I have a bunch of markdown files and I want to get the filename and the line number where a pattern happens. I have tried something like:

awk '/pattern/ && NR > 7 {print FILENAME, NR, $0}' *.md

What happens is that awk considers all files as one and gives the wrong number. I also use zsh and I don't know what I am missing. I already have searched on the web with no avail.

The condition for listing the file is that the pattern happens after line seven in all markdown files.

kvantour
  • 25,269
  • 4
  • 47
  • 72
SergioAraujo
  • 11,069
  • 3
  • 50
  • 40
  • 1
    awk has the internal variable `NR` which is the total `record number` of all records currently processed, accumulative over multiple files. There is also the internal variable `FNR` which is the record number of the current file. It is the latter you are interested in. – kvantour Dec 22 '19 at 09:52

1 Answers1

1

Use FNR, which is the record (line) number of the current input file.

awk '/pattern/ && FNR > 7 {print FILENAME, FNR, $0}' *.md
chepner
  • 497,756
  • 71
  • 530
  • 681