xargs是一個非常有用的命令。下面給出我常用的幾個例子:
假設有文件 f 內容如下:
abc
123
456
789
qwe
rty
op
test命令把命令行輸入原樣輸出,源碼如下:
#include <stdio.h>
int main(int argc, char **argv)
{
for(int i=0; i<argc; ++i)
printf("%s ", argv[i]);
printf("\n");
return 0;
}
cat f|xargs ./test
輸出:
./test abc 123 456 789 qwe rty op
cat f|xargs -n 1 ./test
輸出:
./test abc
./test 123
./test 456
./test 789
./test qwe
./test rty
./test op
cat f|xargs -n 1 ./test xxx
輸出:
./test xxx abc
./test xxx 123
./test xxx 456
./test xxx 789
./test xxx qwe
./test xxx rty
./test xxx op
cat f|xargs -I aaa -n 1 ./test aaa xxx
輸出:
./test abc xxx
./test 123 xxx
./test 456 xxx
./test 789 xxx
./test qwe xxx
./test rty xxx
./test op xxx
cat f|xargs -i -n 1 ./test {} xxx
輸出:
./test abc xxx
./test 123 xxx
./test 456 xxx
./test 789 xxx
./test qwe xxx
./test rty xxx
./test op xxx