LoginSignup
1
0

More than 3 years have passed since last update.

Makefileでファイルの中身を一行ずつ処理する方法

Posted at

ファイルの中身を一行ずつ処理する場合、シェルで処理するだけだったら、これでおk。

cat a.txt | while read line
do
    echo $line
done

ところが、Makefileでこれをやると怒られる。
怒られずに同じことをやるには、xargsを使うとよい。
以下のように書くと、ファイルの中身が一行ずつechoの引数に渡されて実行される。

cat a.txt | xargs -L 1 -i echo {}
a.txt
1+1
2+2
3+3
出力
1+1
2+2
3+3

引数としてではなく、標準入力として渡したい場合、echoを経由してパイプすればよい。

cat a.txt | xargs -L 1 -i echo {}|bc
a.txt
1+1
2+2
3+3
出力
2
4
6

後者はちょっと回りくどいので、いい方法を知っていたらぜひ教えてください。

1
0
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
1
0