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

后端 未结 11 1285
后悔当初
后悔当初 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:20

    An extension to Joachim Isaksson's answer: Quite often I need something from the middle of a long file, e.g. lines 5001 to 5020, in which case you can combine head with tail:

    head -5020 file.txt | tail -20 | grep x
    

    This gets the first 5020 lines, then shows only the last 20 of those, then pipes everything to grep.

    (Edited: fencepost error in my example numbers, added pipe to grep)

提交回复
热议问题