xargs 在输出到新行时在 echo 语句后添加空格

xargs adds whitespace after echo statement when outputting on to new line

使用 xargs 和 echo 将 samtools 的结果输出到 output.txt 文件中的新行

samtools view $SAMPLE\.bam | cut -f3 | uniq -c | sort -n | \
xargs -r0 -n1 echo -e "Summarise mapping...\n" >> ../output.txt

这会将结果添加到 echo 之后的新行,但还会在第一行的结果之前添加 space,我该如何停止呢?

不是 xargs 添加了 space。这是 echo 命令:

The echo utility arguments shall be separated by single <space> characters and a <newline> character shall follow the last argument. (Text from Posix standard; emphasis added.)

如果您想要更多控制,请使用 printf:

...
xargs -r0 -n1 printf "Summarise mapping...\n%s\n" >> ../output.txt

不像printf不会自动在末尾添加换行符,所以需要在格式中包含。

请注意,printf 会自动将格式字符串中的转义序列解释为 \n(但不会在内插参数中)。作为使用 printf 的额外好处,您可以省略 -n1 选项,因为 printf 会自动重复格式,直到所有参数都被消耗。