[英]How to concatenate identifier specified on two rows?
Input where identifier specified by two rows 1-2 输入由两行1-2指定的标识符
L1_I L1_I C-14 <---| unique idenfier
WWPTH WWPT WWPTH <---| on two rows
1 2 3
Goal: how to concatenate the rows? 目标:如何串联行?
L1_IWWPTH L1_IWWPT C-14WWPTH <--- unique identifier
1 2 3
Ps I will accept the simplest and most elegant solution. 附言:我将接受最简单,最优雅的解决方案。
Assuming that the input is in a file called file
: 假设输入在名为file
:
$ awk 'NR==1{for (i=1;i<=NF;i++) a[i]=$i;next} NR==2{for (i=1;i<=NF;i++) printf "%-20s",a[i] $i;print"";next} 1' file
L1_IWWPTH L1_IWWPT C-14WWPTH
1 2 3
NR==1{for (i=1;i<=NF;i++) a[i]=$i;next}
For the first line, save all the column headings in the array a
. 对于第一行,将所有列标题保存在数组a
。 Then, skip over the rest of the commands and jump to the next line. 然后,跳过其余命令,并跳至下一行。
NR==2{for (i=1;i<=NF;i++) printf "%-20s",a[i] $i;print"";next}
For the second line, print all the column headings, merging together the ones from the first and second rows. 对于第二行,打印所有列标题,将第一行和第二行中的标题合并在一起。 Then, skip over the rest of the commands and jump to the next line. 然后,跳过其余命令,并跳至下一行。
1
1
is awk's cryptic shorthand for print the line as is. 1
是awk照原样打印行的隐喻速记。 This is done for all lines after the seconds. 在几秒钟后对所有行执行此操作。
If columns are tab-separated: 如果列用制表符分隔:
awk -F'\t' 'NR==1{for (i=1;i<=NF;i++) a[i]=$i;next} NR==2{for (i=1;i<=NF;i++) printf "%s\t",a[i] $i;print"";next} 1' file
If you plan to use python, you can use zip in the following way: 如果计划使用python,则可以通过以下方式使用zip:
input = [['L1_I', 'L1_I', 'C-14'], ['WWPTH','WWPT','WWPTH'],[1,2,3]]
output = [[i+j for i,j in zip(input[0],input[1])]] + input[2:]
print output
output: 输出:
[['L1_IWWPTH', 'L1_IWWPT', 'C-14WWPTH'], [1, 2, 3]]
#!/usr/bin/awk -f NR == 1 { split($0, a) next } NR == 2 { for (b in a) printf "%-20s", a[b] $b print "" next } 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.