私はgrepの使い方を学んでいますが、これを見つけました。どのように動作しますか? [閉鎖]

私はgrepの使い方を学んでいますが、これを見つけました。どのように動作しますか? [閉鎖]

どうなりますか?

grep '#' input.txt | tail -n

私に与える?私はこれに慣れておらず、一緒に使用するgreptail何が起こるのかわかりません。

ベストアンサー1

grep '#' input.txt

このコマンドはファイルからテキストを検索します。この場合、#input.txt ファイルから検索し、すべての項目が画面に印刷されます。

その後、パイプ記号を使用しました。これは、出力を印刷せずにパイプシンボルの後に作成され、次のコマンドに出力を渡すことを意味します。

だから

tail -n

tail -n は、一番下の数字で行数を印刷するために数字が必要なため、正しくありません。つまり、tail -n 2最後の2行を印刷します。

この例では、次の内容が記録された input.txt というファイルがあります。

[root@localhost student]# cat input.txt 
# this is a new line comment 1
this is not a comment line 1
this is not a comment line 2

# this is a new line comment 2
# this is a new line comment 3 
# this is a new line comment 4 
# this is a new line comment 5 
# this is a new line comment 6 
# this is a new line comment 7 
# this is a new line comment 8 
# this is a new line comment 9 
# this is a new line comment 10 

this is not a comment line 3
this is not a comment line 4
this is not a comment line 5

コマンドを実行すると

[root@localhost student]# grep '#' input.txt | tail -n 2
# this is a new line comment 9 
# this is a new line comment 10 

ファイル内の文字を検索して10行程度を検索し、さらにフィルタリングし、tail -n 2を作成して結果の最後の2行を印刷します。

今それがあなたに明確になることを願っています

おすすめ記事