如何串联两行上指定的标识符? [英] How to concatenate identifier specified on two rows?

查看:107
本文介绍了如何串联两行上指定的标识符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入,其中由两行1-2指定的标识符

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

目标:

L1_IWWPTH           L1_IWWPT            C-14WWPTH          <--- unique identifier
1                   2                   3

Ps我将接受最简单,最优雅的解决方案。

推荐答案

假定输入位于名为<$的文件中c $ c> 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}

    对于第一行,将所有列标题保存在数组 a 。然后,跳过其余命令,然后跳到下一行。

    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 是awk的隐喻速记,用于按原样打印行。在几秒钟后对所有行执行此操作。

    1 is awk's cryptic shorthand for print the line as is. 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
    

    这篇关于如何串联两行上指定的标识符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆