我正在尝试使用awk来转动文件。 输入文件:
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_004 20010090fa180e48
-----------------------------
FA-1D_005 20040090fa180e48
-----------------------------
FA-1D_006 10000090fa180e48
20020090fa180e48
20020090fa180e49
20030090fa180e49
-----------------------------
FA-1D_011 10000090fa180e48
-----------------------------
FA-2D_004 20020090fa180e48
20010090fa180e49
-----------------------------
FA-2D_006 10000090fa180e48
10000090fa180e49
20010090fa180e48
20040090fa180e49
-----------------------------
FA-2D_008 20010090fa180e49
-----------------------------
FA-2D_009 20040090fa180e49
-----------------------------
FA-2D_011 20010090fa180e48
-----------------------------
FA-2D_011 10000090fa180e49
20010090fa180e48
-----------------------------
FA-3D_004 10000090fa180e49
20010090fa180e49
20010090fa180e48
20020090fa180e48
-----------------------------
FA-3D_005 10000090fa180e48
20040090fa180e49
20030090fa180e48
-----------------------------
FA-3D_006 20010090fa180e48
-----------------------------
FA-3D_007 20030090fa180e48
-----------------------------
FA-4D_004 20020090fa180e49
20030090fa180e49
20020090fa180e48
-----------------------------
FA-4D_005 10000090fa180e49
20010090fa180e48
-----------------------------
FA-4D_006 10000090fa180e48
-----------------------------
请知道如何使用awk创建枢轴以形成所需的输出。
答案 0 :(得分:0)
如果订单无关紧要
以下是单行:
awk '{k=$1 FS $2; if(!(k in t)){a[$1]=($1 in a? a[$1] RS sprintf("%*s",length($1)+1,"") :"")$2; t[k]}}END{s=sprintf("%0*s\n", length(k),""); gsub(/0/,"-",s); for(i in a)print s i,a[i] }' file
如果添加 GNU awk ,您将获得正确的订单,
You may try here by pasting given data, in stdin tab
BEGIN{
PROCINFO["sorted_in"]="@ind_str_asc"
}
更好的可读性:
awk 'BEGIN{
PROCINFO["sorted_in"]="@ind_str_asc"
}
{
k=$1 FS $2;
if(!(k in t))
{
a[$1]=($1 in a? a[$1] RS sprintf("%*s",length($1)+1,"") :"")$2;
t[k]
}
}
END{
s=sprintf("%0*s\n", length(k),"");
gsub(/0/,"-",s);
for(i in a)print s i,a[i]
}
' file
$ cat file
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
会导致 - 使用GNU awk:
--------------------------
FA-1D_004 20010090fa180e48
--------------------------
FA-1D_005 20040090fa180e48
--------------------------
FA-1D_006 20020090fa180e49
20030090fa180e49
10000090fa180e48
20020090fa180e48
--------------------------
FA-1D_011 10000090fa180e48
--------------------------
FA-2D_004 20010090fa180e49
20020090fa180e48
--------------------------
FA-2D_006 10000090fa180e49
20040090fa180e49
10000090fa180e48
20010090fa180e48
--------------------------
FA-2D_008 20010090fa180e49
--------------------------
FA-2D_009 20040090fa180e49
--------------------------
FA-2D_010 20020090fa180e48
--------------------------
FA-2D_011 10000090fa180e49
20010090fa180e48
--------------------------
FA-3D_004 10000090fa180e49
20010090fa180e49
20010090fa180e48
20020090fa180e48
--------------------------
FA-3D_005 20040090fa180e49
10000090fa180e48
20030090fa180e48
--------------------------
FA-3D_006 20010090fa180e48
--------------------------
FA-3D_007 20030090fa180e48
--------------------------
FA-4D_004 20020090fa180e49
20030090fa180e49
20020090fa180e48
--------------------------
FA-4D_005 10000090fa180e49
20010090fa180e48
--------------------------
FA-4D_006 10000090fa180e48
答案 1 :(得分:0)
关注awk
可能对您有帮助,它会通过从第一个字段的第二个字符串(如1D
或2D
等进行排序来为您提供输出:
sort -t"-" -k1.1 Input_file | awk -v s1="-----------------------------" '{a[$1]=a[$1]? a[$1] RS "\t\t" $NF:$NF} !b[$1]++{c[++i]=$1} END{for(j=1;j<=i;j++){print s1 RS c[j]"\t"a[c[j]]}}'
现在添加一种非单线形式的解决方案:
sort -t"-" -k1.1 Input_file |
awk -v s1="-----------------------------" '
{
a[$1]=a[$1]? a[$1] RS "\t\t" $NF:$NF
}
!b[$1]++{
c[++i]=$1
}
END{
for(j=1;j<=i;j++){
print s1 RS c[j]"\t"a[c[j]]}
}'
输出如下:
-----------------------------
FA-1D_004 20010090fa180e48
-----------------------------
FA-1D_005 20040090fa180e48
-----------------------------
FA-1D_006 10000090fa180e48
20020090fa180e48
20020090fa180e49
20030090fa180e49
-----------------------------
FA-1D_011 10000090fa180e48
-----------------------------
FA-2D_004 20010090fa180e49
20020090fa180e48
-----------------------------
FA-2D_006 10000090fa180e48
10000090fa180e49
20010090fa180e48
20040090fa180e49
-----------------------------
FA-2D_008 20010090fa180e49
-----------------------------
FA-2D_009 20040090fa180e49
-----------------------------
FA-2D_010 20020090fa180e48
-----------------------------
FA-2D_011 10000090fa180e49
20010090fa180e48
-----------------------------
FA-3D_004 10000090fa180e49
20010090fa180e48
20010090fa180e49
20020090fa180e48
-----------------------------
FA-3D_005 10000090fa180e48
20030090fa180e48
20040090fa180e49
-----------------------------
FA-3D_006 20010090fa180e48
-----------------------------
FA-3D_007 20030090fa180e48
-----------------------------
FA-4D_004 20020090fa180e48
20020090fa180e49
20030090fa180e49
-----------------------------
FA-4D_005 10000090fa180e49
20010090fa180e48
-----------------------------
FA-4D_006 10000090fa180e48