1、# cat a.txt
codeA ref 1
codeB ref 3
codeA rex 5
codeB rex 2
输出格式为:
codeA
ref:1
rex:5
codeB
rex:2
ref:3
方法一、
cat a.txt | sort +0 -1 +2n | awk '{print $1,$2":"$3}' | awk '{a[$1]=a[$1]?a[$1]"\n"$2:$1"\n"$2}END{for(i in a)print a[i]}'
方法二、
cat b.txt | sort +0 -1 +2nr | awk '{print $1,$2":"$3}' | awk '{print $1,$2":"$3}' | awk '{if(!a[$1]++){print $1"\n"$2;}else{print $2}}'
方法三、
cat b.txt | sort +0 -1 +2nr | awk '{print $1,$2":"$3}' | awk '{print $1==i?$2:$1 RS $2;i=$1}'
2、行转列
oracle@/opt/oracle/zch:~>cat 3.txt
111
222
333
444
555
666
777
oracle@/opt/oracle/zch:~>awk 'BEGIN{printf ""}{printf $1" "}END{printf ""}' 3.txt
111 222 333 444 555 666 777 oracle@/opt/oracle/zch:~>
codeA ref 1
codeB ref 3
codeA rex 5
codeB rex 2
输出格式为:
codeA
ref:1
rex:5
codeB
rex:2
ref:3
方法一、
cat a.txt | sort +0 -1 +2n | awk '{print $1,$2":"$3}' | awk '{a[$1]=a[$1]?a[$1]"\n"$2:$1"\n"$2}END{for(i in a)print a[i]}'
方法二、
cat b.txt | sort +0 -1 +2nr | awk '{print $1,$2":"$3}' | awk '{print $1,$2":"$3}' | awk '{if(!a[$1]++){print $1"\n"$2;}else{print $2}}'
方法三、
cat b.txt | sort +0 -1 +2nr | awk '{print $1,$2":"$3}' | awk '{print $1==i?$2:$1 RS $2;i=$1}'
2、行转列
oracle@/opt/oracle/zch:~>cat 3.txt
111
222
333
444
555
666
777
oracle@/opt/oracle/zch:~>awk 'BEGIN{printf ""}{printf $1" "}END{printf ""}' 3.txt
111 222 333 444 555 666 777 oracle@/opt/oracle/zch:~>