Unix -“xargs”-输出“在中间”(而不是在最后!)

rslzwgfq  于 2023-10-18  发布在  Unix
关注(0)|答案(4)|浏览(273)

在Unix中使用xargs应用程序的示例如下:

  1. ls | xargs echo

这是相同的(假设我在工作目录中有someFilesomeDir/):

  1. echo someFile someDir

所以xargs获取它的输入并将其放在下一个命令的末尾(这里是echo的末尾)。
但有时我希望xargs将其输入放在下一个命令的中间位置。
举例来说:

  1. find . -type f -name "*.cpp" -print | xargs g++ -o outputFile

因此,如果我在当前目录中有a.cppb.cppc.cpp文件,输出将与命令相同:

  1. g++ -o outputFile a.cpp b.cpp c.cpp

但我想要这样的东西

  1. g++ a.cpp b.cpp c.cpp -o outputFile

有办法做到吗?
PS:在某些情况下,我需要它,因为例如:

  1. i586-mingw32msvc-g++ -o outputFile `pkg-config --cflags --libs gtkmm-2.4` a.cpp b.cpp c.cpp

不工作,但这一个工作得很好:

  1. i586-mingw32msvc-g++ a.cpp b.cpp c.cpp -o outputFile `pkg-config --cflags --libs gtkmm-2.4`
xzlaal3s

xzlaal3s1#

如果您的xargs版本不包含-I功能,那么另一种方法是编写一个包含您想要执行的命令的小shell脚本:

  1. #!/bin/sh
  2. exec i586-mingw32msvc-g++ "$@" -o outputFile...

然后使用xargs运行:

  1. find . -type f -name "*.cpp" -print | xargs my_gcc_script
mlnl4t2r

mlnl4t2r2#

您不需要xargs来执行此操作。只需用途:

  1. g++ `find . -type f -name '*.cpp'` -o outputFile
slwdgvem

slwdgvem3#

GNU Parallel http://www.gnu.org/software/parallel/也是一个解决方案:

  1. find . -type f -name "*.cpp" -print | parallel -Xj1 g++ {} -o outputFile
  • .cpp必须适合单行(~128 KB)。
twh00eeo

twh00eeo4#

为了回答标题中提出的原始问题,如何使用xargs将输入放在中间而不是结尾:

  1. $ echo a b c | xargs -I {} echo before {} after
  2. before a b c after

这将用管道输出替换命令中的{}。BSD和GNU xargs之间有一些细微的区别,如下所述:

BSD xargs(例如在MacOS/达尔文,freebsd)

使用-I REPLACE,它将替换命令中的字符串REPLACE(或您传递的任何内容)。举例来说:

  1. $ echo a b c | xargs -I {} echo before {} after
  2. before a b c after
  3. $ echo a b c | xargs -I REPLACE echo before REPLACE after
  4. before a b c after
  5. $ echo 'a
  6. > b
  7. > c' | xargs -L1 -I {} echo before {} after
  8. before a after
  9. before b after
  10. before c after

man page描述了该选项:

  1. -I replstr
  2. Execute utility for each input line, replacing one or more occur-
  3. rences of replstr in up to replacements (or 5 if no -R flag is
  4. specified) arguments to utility with the entire line of input.
  5. The resulting arguments, after replacement is done, will not be
  6. allowed to grow beyond replsize (or 255 if no -S flag is speci-
  7. fied) bytes; this is implemented by concatenating as much of the
  8. argument containing replstr as possible, to the constructed argu-
  9. ments to utility, up to replsize bytes. The size limit does not
  10. apply to arguments to utility which do not contain replstr, and
  11. furthermore, no replacement will be done on utility itself.
  12. Implies -x.

GNU xargs(例如:在Linux上)

  1. $ echo a b c | xargs -i echo before {} after
  2. before a b c after
  3. $ echo a b c | xargs -I THING echo before THING after
  4. before a b c after

使用-I REPLACE-i参数,如the man page所述:

  1. -I replace-str
  2. Replace occurrences of replace-str in the initial-arguments
  3. with names read from standard input. Also, unquoted blanks do
  4. not terminate input items; instead the separator is the
  5. newline character. Implies -x and -L 1.
  6. -i[replace-str], --replace[=replace-str]
  7. This option is a synonym for -Ireplace-str if replace-str is
  8. specified. If the replace-str argument is missing, the effect
  9. is the same as -I{}. This option is deprecated; use -I
  10. instead.

-I上的-L 1意味着它将在单独的命令中执行每个输入:

  1. $ echo "a
  2. > b
  3. > c" | xargs -I THING echo before THING after
  4. before a after
  5. before b after
  6. before c after
展开查看全部

相关问题