2009-08-25 9 views

答えて

15

man grep NEWFILEヘッド-n 5>は

-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. 

はとても1は

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

パイプの必要性を全く使用することができることを言及しています。 grepは、あなたが必要とするほとんどのものをそれ自身でサポートしています。

+0

ありがとう! '-m'について知らなかった。 – seth

7
grep model [file] | head -n 5 > [newfile] 
-1
cat file | grep model | head -n 5 > outfile.txt 
+5

これは恐ろしいことです。 'cat'を使うと' grep'を実行する前にまずファイル全体をメモリにロードします。ファイルパラメータで 'grep'を呼び出してください。 –

+1

他の答えは良いですが、上記のコメントは当てはまりません。シェルは、最初に出力を待たずに3つのコマンドをすべて開始します。 – mark4o

+1

Windowsでは、パイプはコマンドラインで使用できますが、最初のプログラムが実行され、ファイルに書き込まれるということがあります。その後、次のプログラムが実行されます。それはLinux/Unixの仕組みではありません。 Unixは脳が壊れているわけではありません。そして、どちらもファイル全体を最初にメモリに読み込まない。あなたはそのアイデアをどこで手に入れましたか? – xcramps

関連する問題