How to concatenate identifier specified on two rows?

雨燕双飞 提交于 2019-12-01 11:13:35

问题


Input where identifier specified by two rows 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

P.s. I will accept the simplest and most elegant solution.


回答1:


Assuming that the input is in a file called 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

How it works

  • 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. 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. This is done for all lines after the seconds.

Tab-separated columns with possible missing columns

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



回答2:


If you plan to use python, you can use zip in the following way:

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]]



回答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


来源:https://stackoverflow.com/questions/26857467/how-to-concatenate-identifier-specified-on-two-rows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!