39
Go to list of users who liked
30
Share on X(Twitter)
Share on Facebook
More than 5 years have passed since last update.
xargs と- の違いについて簡潔にかきたいと思います。
たとえば、
例1
$find ./*.txt | xargsgrep'hoge'とか、
例2
$sed's/a/c/g' sample_a.txt | diff - sample_b.txt1c1< This is c---> This is bなど、パイプにからめて使うことがあります。
違いについて
xargsは、標準入力を引数のリストとして解釈します。-は、標準入力の内容を、引数(ファイル)の内容として解釈します。
例1は...
findはファイルのリストを返します。
$bash-3.2$find ./*.txt./sample_a.txt./sample_b.txtそして、grep は下のように、そのファイルリストをそのまま引数として受取りたいところです。
grep 'hoge' file1.txt file2.txtですので、xargs を使います。
例2は...
diff は通常、以下のようにファイル名を引数に持ちます。
diff file1.txt file2.txtですが、- によって、標準入力を使うこともできます。
$ diff sample_a.txt -hoge1,2c1< This is a< hello---> hogeそして、パイプと絡めます。
パイプは、パイプ前部の標準出力を、パイプ後部に標準入力に渡してくれるものです。
ですので、以下のような書き方が有効になります。
sed 's/a/c/g' sample_a.txt | diff - sample_b.txtRegister as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme
