我有四个文件(file_one
至file_four
),这些文件的内容并不是很重要。我想按数组定义的特定顺序将其中三个文件传递给命令(即paste
或awk
)。我的数组是order=(two one four)
。我希望使用数组将所需的文件作为输入传递给命令,就像您可以使用的那样*
(即paste file_* > combined_file
);
paste file_${order[@]} > combined_file
paste file_"${order[@]}" > combined_file
paste file_"${order[*]}" > combined_file
paste file_"{${order[@]}}" > combined_file
paste file_{"${order[@]}"} > combined_file
我看过不同的页面(1,2,3或4),但我无法得到它的工作。loop
在paste
或的情况下,文件中的A无效awk
。我想一次将所有文件传递给命令。鉴于我的UNIX
知识有限,我可能误解了一些解决方案/答案。从我从这个答案可以理解的角度来看,数组最初是如何开发的bash
。
所需结果: paste file_two file_one file_four > combined_file
您可以使用printf
:
paste $(printf "file_%s " ${order[@]}) > combined_file
这避免了必须遍历order
数组的所有元素。
或者,使用bash ism,您可以使用以下命令:
paste ${order[@]/#/file_} > combined_file
请注意,#
它与bash手册页中提到的模式的开头相匹配:
$ {parameter / pattern / string}
(...)如果pattern以#开头,则它必须在参数的扩展值的开头匹配。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句