LoginSignup
2
4

More than 5 years have passed since last update.

[awk] 複数ファイルへの出力

Posted at

awkで複数のファイルへデータを出力する方法です。

入力データ例

$ cat test1.txt
1
2
3
4
5

リダイレクト演算子で出力するファイルを指定

データが偶数の場合はtest2.txtに、奇数の場合はtest3.txtに出力されるようにしています。

awk '{
    if (($1%2) == 0) print $1 > "test2.txt";
    else print $1 > "test3.txt";
}' test1.txt

結果の確認

$ cat test2.txt
2
4
$ cat test3.txt
1
3
5
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4