- 论坛徽章:
- 93
|
吃个午饭,消化一下:- [seesea@UC ~]$ awk -f xx.awk a.txt
- A产品编号 A价格 B产品编号 B价格 C产品编号 C价格
- 001 10 004 40 006 60
- 002 20 005 50 007 70
- 003 30
- [seesea@UC ~]$ cat xx.awk
- BEGIN {
- OFS = "\t"
- }
- NR == 1 {
- title1 = $2
- title2 = $3
- }
- NR != 1 {
- if (! ar_count[$1])
- ar_count[$1] = 0
- ar[$1, ar_count[$1], title1] = $2
- ar[$1, ar_count[$1], title2] = $3
- ar_count[$1]++
- }
- END {
- asorti(ar_count, ar_type)
- len = asort(ar_count)
- for (i in ar_type)
- {
- printf ar_type[i] title1 OFS ar_type[i] title2 OFS
- }
- printf ORS
- for (j = 0; j < ar_count[len]; ++j)
- {
- for (i in ar_type)
- {
- printf ar[ar_type[i], j, title1] OFS ar[ar_type[i], j, title2] OFS
- }
- printf ORS
- }
- }
复制代码 |
|