简体   繁体   English

获取与特定模式匹配的前n行(使用Linux命令)

[英]Get the first n lines matching a certain pattern (with Linux commands)

I have a giant file where I want to find a term model . 我有一个巨大的文件,我想找到一个术语模型 I want to pipe the first 5 lines containing the word model to another file. 我想将包含单词model的前5行管道传输到另一个文件。 How do I do that using Linux commands? 如何使用Linux命令执行此操作?

man grep mentions that man grep提到那个

 -m NUM, --max-count=NUM
          Stop reading a file after NUM matching lines.  If the  input  is
          standard  input  from a regular file, and NUM matching lines are
          output, grep ensures that the standard input  is  positioned  to
          just  after the last matching line before exiting, regardless of
          the presence of trailing context lines.  This enables a  calling
          process  to resume a search. 

so one can use 所以可以使用

grep model old_file_name.txt -m 5 > new_file_name.txt

No need for a pipe. 不需要管道。 grep supports almost everything you need on it's own. grep支持几乎所有你需要的东西。

grep model [file] | head -n 5 > [newfile]

grep "model" filename | grep“model”文件名| head -n 5 > newfile head -n 5> newfile

cat file | grep model | head -n 5 > outfile.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM