无法替换冗余cat的重定向

时间:2014-12-21 00:51:21

标签: bash cut io-redirection xargs

cat  joined.txt | xargs -t -a <(cut --fields=1 | sort -u | grep -E '\S') -I{} --max-args=1 --max-procs=4 echo "mkdir -p imdb/movies/{}; grep '^{}' joined.txt  > imdb/movies/{}/movies.txt" | bash

上面的代码可以工作,但是在代码开头用冗余的cat替换下面的重定向并不起作用,导致切换输入输出错误。

< joined.txt xargs -t -a <(cut --fields=1 | sort -u | grep -E '\S') -I{} --max-args=1 --max-procs=4 echo "mkdir -p imdb/movies/{}; grep '^{}' joined.txt  > imdb/movies/{}/movies.txt" | bash

1 个答案:

答案 0 :(得分:2)

在任何一种情况下,进程替换中的cut命令(而不是xargs)应该从joined.txt读取,所以为了完全安全,你应该放进程替换中的管道或输入重定向。实际上,两者都没有必要; cut可以将joined.txt作为参数。

xargs -t -a <( cat joined.txt | cut ... ) ... | bash

xargs -t -a <( cut -f1 joined.txt | ... ) ... | bash

但是,最简单的做法是完全跳过进程替换,并将该管道的输出传递给xargs

cut -f joined.txt | sort -u | grep -E '\S' | xargs -t ...