linux コマンド パイプの使い方

スポンサーリンク

バイオインフォ道場、くまぞうです。

パイプはコマンドの処理を左から右へつなぐので、パイプライン処理と言います。「|」(パイプ:vertical line)の左側で実行したコマンドの結果を、「|」の右側のコマンドの入力として処理します。

スポンサーリンク



書き方の例を示します。

$ command1 | command2

パイプを使わない場合は以下のようになります。
中間ファイル不要・並列して動作しないという点が異なります。

$ command1 > tmpfile; command2 < tmpfile

パイプを使う

簡単な例でパイプを使ってみましょう。

例1. 「echo」で出力した文字を「wc」でカウントします。

$ echo bioinformatics | wc
1 1 15  # line word byte
$

例2. ファイルを表示して、ソート後、ユニークなものだけを表示

$ cat file1
1 bioinformatics aaa
2 bioinformatics bbb
1 bioinformatics aaa
4 bioinfo aaa

$ cat file1 | sort | uniq
1 bioinformatics aaa
2 bioinformatics bbb
4 bioinfo aaa
スポンサーリンク



タイトルとURLをコピーしました