![私はgrepの使い方を学んでいますが、これを見つけました。どのように動作しますか? [閉鎖]](https://linux33.com/image/187485/%E7%A7%81%E3%81%AFgrep%E3%81%AE%E4%BD%BF%E3%81%84%E6%96%B9%E3%82%92%E5%AD%A6%E3%82%93%E3%81%A7%E3%81%84%E3%81%BE%E3%81%99%E3%81%8C%E3%80%81%E3%81%93%E3%82%8C%E3%82%92%E8%A6%8B%E3%81%A4%E3%81%91%E3%81%BE%E3%81%97%E3%81%9F%E3%80%82%E3%81%A9%E3%81%AE%E3%82%88%E3%81%86%E3%81%AB%E5%8B%95%E4%BD%9C%E3%81%97%E3%81%BE%E3%81%99%E3%81%8B%EF%BC%9F%20%5B%E9%96%89%E9%8E%96%5D.png)
どうなりますか?
grep '#' input.txt | tail -n
私に与える?私はこれに慣れておらず、一緒に使用するgrep
とtail
何が起こるのかわかりません。
答え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行を印刷します。
今それがあなたに明確になることを願っています