Can I grep only the first n lines of a file?

后端 未结 11 1009
后悔当初
后悔当初 2021-01-30 07:33

I have very long log files, is it possible to ask grep to only search the first 10 lines?

11条回答
  •  时光说笑
    2021-01-30 08:14

    For folks who find this on Google, I needed to search the first n lines of multiple files, but to only print the matching filenames. I used

     gawk 'FNR>10 {nextfile} /pattern/ { print FILENAME ; nextfile }' filenames
    

    The FNR..nextfile stops processing a file once 10 lines have been seen. The //..{} prints the filename and moves on whenever the first match in a given file shows up. To quote the filenames for the benefit of other programs, use

     gawk 'FNR>10 {nextfile} /pattern/ { print "\"" FILENAME "\"" ; nextfile }' filenames
    

提交回复
热议问题