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

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

    Or use awk for a single process without |:

    awk '/your_regexp/ && NR < 11' INPUTFILE
    

    On each line, if your_regexp matches, and the number of records (lines) is less than 11, it executes the default action (which is printing the input line).

    Or use sed:

    sed -n '/your_regexp/p;10q' INPUTFILE 
    

    Checks your regexp and prints the line (-n means don't print the input, which is otherwise the default), and quits right after the 10th line.

提交回复
热议问题