我有一个变量,其中包含几个其他变量的名称列表。这些变量每个都包含一个表。我想加入所有这些表格。
表格看起来像这样:
Name Average Name Average
A 1 A 1.1
B 2 B 2.2
C 3 C 3.3 etc.
D 4 D 4.4
E 5 E 5.5
我的变量名称列表叫做$ all_variables,这里的内容是什么样的(在实际情况下还有很多变量):
echo "$all_variables"
$table1
$table2
$table3
$table4
$table5
要创建join函数的参数列表,我创建了$ all_variables_join,其中包含join函数的参数:
echo "$all_variables_join"
<(echo "$table1") <(echo "$table2") <(echo "$table3") <(echo "$table4") <(echo "$table5")
然后我想使用以下内容运行join(基于第一列,因此我使用默认选项):
join "$all_variables_join" > file.txt
将扩展为
join <(echo "$table1") <(echo "$table2") <(echo "$table3") <(echo "$table4") <(echo "$table5") > file.txt
file.txt将包含以下内容:
Name Average
A 1 1.1
B 2 2.2
C 3 3.3 etc...
D 4 4.4
E 5 5.5
然而,当我尝试运行时,我收到此错误:
join "$all_variables_join" > file.txt
join: missing operand after `<(echo "$table1") <(echo "$table2") <(echo "table3") <(echo "$table4") <(echo "$table5")'
Try `join --help' for more information.
我知道如何解决这个问题吗?
非常感谢任何帮助!
感谢
编辑:修正了错误信息,我复制了错误的信息
答案 0 :(得分:0)
@giles和@ccarton指出你需要取消$all_variables
周围的双引号。这是一个显示原因的例子:
touch 1
touch 2
x='1 2'
ls $x
ls "$x"
然而,这不会解决您的问题,因为正如@ccarton所说,join
一次只能接受两个文件。
一个可行的策略是创建一个包含所有可能名称(A,B,C ......)的列:
table=$(echo -e "$table1\n$table2\n$table3\n$table4\n$table5" |
tail -n+2 |
awk '{print $1}' |
sort -u)
然后逐个加入每个表:
table=$(join -a1 <(echo "$table") <(echo "$table1"))
table=$(join -a1 <(echo "$table") <(echo "$table2"))
table=$(join -a1 <(echo "$table") <(echo "$table3"))
table=$(join -a1 <(echo "$table") <(echo "$table4"))
table=$(join -a1 <(echo "$table") <(echo "$table5"))
可以使用循环而不是显式命名table1 ... table5,但如果数据是在文件而不是变量中,则最自然的是,例如,
mkdir /tmp/tables
echo "$table1" > /tmp/tables/table1
...
echo "$table5" > /tmp/tables/table1
for t in /tmp/tables/*; do
table=$(join -a1 <(echo "$table") $f)
done
关于join
的两个注释:1。-a
即使在右表中没有匹配也会保留该行。 2.如果密钥已经不存在,则必须对其进行分类:
table=$(join -a1 <(echo "$table") <(sort -k1 $f))