[英]join columns across multiple lines in a Text based on common column using BASH
I have a file like below . 我有一个像下面的文件。
Table1|Column1
Table2|Column1
Table5|Column1
Table3|Column2
Table2|Column2
Table4|Column3
Table2|Column3
Table2|Column4
Table5|Column4
Table2|Column5
From the below file i am trying to generate a Dynamic SQL JOIn if Tablenames in Column1 have same Attributes 如果列1中的表名具有相同的属性,我试图从下面的文件中生成一个动态SQL JOIn
select * from Table1 a inner join Table2 b on a.Column1=b.column1 inner join Table5 c on a.Column1=c.column1
and 和
select * from Table3 a inner join Table2 b on a.column2 = b.column2
..etc till end of file ..etc到文件结尾
What is the best way to do it , please advise. 最好的方法是什么,请告知。
Please note that Same column can appear in more than 2 tables (like upto 20 tables then join will be repeated 19 times) 请注意,同一列可以出现在2个以上的表中(例如最多20个表,那么联接将重复19次)
This is not a complete answer. 这不是一个完整的答案。 However, I think you should be able to solve the problem using this answer as a stepping stone.
但是,我认为您可以使用此答案作为垫脚石来解决问题。
We use GNU awk
for parsing. 我们使用GNU
awk
进行解析。 For better readability we use a script file parse.awk
instead of one long command. 为了提高可读性,我们使用脚本文件
parse.awk
而不是一个长命令。
# parse.awk
{ a[$2][$1] };
END {
for (col in a) {
printf "%s", col;
for (tab in a[col])
printf "|%s", tab;
print ""
}
}
When we call the script ... 当我们调用脚本时...
awk -F\| -f parse.awk yourFile
... on your example the output is ...在您的示例中,输出为
Column1|Table5|Table1|Table2
Column2|Table2|Table3
Column3|Table2|Table4
Column4|Table5|Table2
Column5|Table2
From there you should be able to build your SQL commands. 从那里您应该能够构建SQL命令。 You could even adapt
parse.awk
to generate the SQL commands directly. 您甚至可以修改
parse.awk
来直接生成SQL命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.