我正在尝试使用awk

时间:2017-12-05 15:42:37

标签: linux shell awk scripting solaris

我正在尝试使用awk来转动文件。 输入文件:

FA-1D_005 10000090fa180e49 FA-2D_006 10000090fa180e49 FA-2D_011 10000090fa180e49 FA-3D_004 10000090fa180e49 FA-4D_005 10000090fa180e49 FA-2D_004 20010090fa180e49 FA-2D_008 20010090fa180e49 FA-3D_004 20010090fa180e49 FA-1D_006 20020090fa180e49 FA-4D_004 20020090fa180e49 FA-1D_006 20030090fa180e49 FA-4D_004 20030090fa180e49 FA-2D_006 20040090fa180e49 FA-2D_009 20040090fa180e49 FA-3D_005 20040090fa180e49 FA-1D_006 10000090fa180e48 FA-1D_011 10000090fa180e48 FA-2D_006 10000090fa180e48 FA-3D_005 10000090fa180e48 FA-4D_006 10000090fa180e48 FA-1D_004 20010090fa180e48 FA-2D_006 20010090fa180e48 FA-2D_011 20010090fa180e48 FA-3D_004 20010090fa180e48 FA-3D_006 20010090fa180e48 FA-4D_005 20010090fa180e48 FA-1D_006 20020090fa180e48 FA-2D_004 20020090fa180e48 FA-2D_010 20020090fa180e48 FA-3D_004 20020090fa180e48 FA-4D_004 20020090fa180e48 FA-3D_005 20030090fa180e48 FA-3D_007 20030090fa180e48 FA-1D_005 20040090fa180e48

期望的输出

FA-1D_005 10000090fa180e49 FA-2D_006
FA-2D_011
FA-3D_004

FA-4D_005

FA-2D_004 20010090fa180e49 FA-2D_008

FA-3D_004

FA-1D_006 20020090fa180e49

FA-4D_004

FA-1D_006 20030090fa180e49

FA-4D_004

FA-2D_006 20040090fa180e49 FA-2D_009

FA-3D_005

FA-1D_006 10000090fa180e48 FA-1D_011
FA-2D_006
FA-3D_005

FA-4D_006

FA-1D_004 20010090fa180e48 FA-2D_006
FA-2D_011
FA-3D_004
FA-3D_006

FA-4D_005

FA-1D_006 20020090fa180e48 FA-2D_004
FA-2D_010
FA-3D_004

FA-4D_004

FA-3D_005 20030090fa180e48

FA-3D_007

FA-1D_005 20040090fa180e48

请知道哪个是生成数据透视表的最佳方法? 提前谢谢。

1 个答案:

答案 0 :(得分:1)

awk救援!

$ awk 'NR==1{hr=sprintf("%"length($0)"s"," ");gsub(" ","-",hr)} 
            {print (p==$2?$1:hr "\n" $0); p=$2} 
         END{print hr}' file

---------------------------
FA-1D_005  10000090fa180e49
FA-2D_006
FA-2D_011
FA-3D_004
FA-4D_005
---------------------------
FA-2D_004  20010090fa180e49
FA-2D_008
FA-3D_004
---------------------------
FA-1D_006  20020090fa180e49
FA-4D_004
---------------------------
FA-1D_006  20030090fa180e49
FA-4D_004
---------------------------
FA-2D_006  20040090fa180e49
FA-2D_009
FA-3D_005
---------------------------
FA-1D_006  10000090fa180e48
FA-1D_011
FA-2D_006
FA-3D_005
FA-4D_006
---------------------------
FA-1D_004  20010090fa180e48
FA-2D_006
FA-2D_011
FA-3D_004
FA-3D_006
FA-4D_005
---------------------------
FA-1D_006  20020090fa180e48
FA-2D_004
FA-2D_010
FA-3D_004
FA-4D_004
---------------------------
FA-3D_005  20030090fa180e48
FA-3D_007
---------------------------
FA-1D_005  20040090fa180e48
---------------------------