shell 行转列

shell 行转列

源文本如下:

a1 a2 a3 1 2 3 11 22 33

输出的文本:

a1:1
a1:11
a2:2
a2:22
a3:3
a3:33

命令:

[root@SourceGG106 rkdir]# echo '
> a1 a2 a3 > 1 2 3 > 11 22 33 >
' |awk -F'<|>' '{if(NF>3){print $3}}' | awk -F " +" '{for(i=1;i<=NF;i++) a[i,NR]=$i}END{for(i=1;i<=NF;i++) {for(j=1;j<=NR;j++) printf a[i,j] " ";print ""}}' | awk '{print $1":"$2"\n"$1":"$3}' | sort a1:1 a1:11 a2:2 a2:22 a3:3 a3:33

代码:

echo '

 
a1 a2 a3 
1 2 3 
11 22 33 
' |awk -F'<|>' '{if(NF>3){print $3}}' | awk -F " +" '{for(i=1;i<=NF;i++) a[i,NR]=$i}END{for(i=1;i<=NF;i++) {for(j=1;j<=NR;j++) printf a[i,j] " ";print ""}}' | awk '{print $1":"$2"\n"$1":"$3}' | sort

行转列代码:

awk -F " +" '{for(i=1;i<=NF;i++) a[i,NR]=$i}END{for(i=1;i<=NF;i++) {for(j=1;j<=NR;j++) printf a[i,j] " ";print ""}}'


你可能感兴趣的:(shell,script)