bash 我可以只 grep 文件的前 n 行吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8762274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I grep only the first n lines of a file?
提问by David LeBauer
I have very long log files, is it possible to ask grep to only search the first 10 lines?
我有很长的日志文件,是否可以要求 grep 只搜索前 10 行?
回答by Joachim Isaksson
The magic of pipes;
管道的魔力;
head -10 log.txt | grep <whatever>
回答by cxw
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
对于在谷歌上找到这个的人,我需要搜索n
多个文件的第一行,但只打印匹配的文件名。我用了
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
FNR..nextfile
一旦看到 10 行,就会停止处理文件。该//..{}
上打印的文件名和移动,每当在给定的文件显示了第一场比赛。要引用文件名以使其他程序受益,请使用
gawk 'FNR>10 {nextfile} /pattern/ { print "\"" FILENAME "\"" ; nextfile }' filenames
回答by Zsolt Botykai
Or use awk
for a single process without |
:
或awk
用于单个进程而没有|
:
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).
在每一行上,如果your_regexp
匹配,并且记录(行)数小于 11,则执行默认操作(即打印输入行)。
Or use sed
:
或使用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.
检查您的正则表达式并打印该行(-n
意味着不打印输入,否则为默认值),并在第 10 行之后立即退出。
回答by Dan Fego
You have a few options using programs along with grep
. The simplest in my opinion is to use head
:
您有几个选项可以使用程序和grep
. 我认为最简单的方法是使用head
:
head -n10 filename | grep ...
head
will output the first 10 lines (using the -n
option), and then you can pipe that output to grep
.
head
将输出前 10 行(使用-n
选项),然后您可以将该输出通过管道传输到grep
.
回答by jaypal singh
grep "pattern" <(head -n 10 filename)
回答by Dileepa Chandima
grep -m6 "string" cov.txt
This searches only the first 6 lines for string
这仅搜索前 6 行 string
回答by vins
head -10 log.txt | grep -A 2 -B 2 pattern_to_search
-A 2
: print two lines before the pattern.
-A 2
: 在图案前打印两行。
-B 2
: print two lines after the pattern.
-B 2
:在图案后打印两行。
head -10 log.txt # read the first 10 lines of the file.
回答by Alan Haggai Alavi
The output of head -10 file
can be piped to grep
in order to accomplish this:
的输出head -10 file
可以通过管道传输grep
以完成此操作:
head -10 file | grep …
Using Perl:
使用 Perl:
perl -ne 'last if $. > 10; print if /pattern/' file
回答by Gustavo Straube
You can use the following line:
您可以使用以下行:
head -n 10 /path/to/file | grep [...]
回答by RoG
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
:
Joachim Isaksson 答案的扩展:我经常需要长文件中间的一些内容,例如第 5001 行到 5020 行,在这种情况下,您可以结合head
使用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.
这将获取前 5020 行,然后仅显示其中的最后 20 行,然后将所有内容通过管道传送到 grep。
(Edited: fencepost error in my example numbers, added pipe to grep)
(编辑:我的示例数字中的围栏错误,向 grep 添加了管道)