[root@159 shell]# cat fl
ID Name Age City Country Tel Salary Children
1001 Steven 25 NY U.S.A +01-02-323222 $4900 2
1002 Huang-Yu 30 BJ CHN +86-10-36789966 ¥6000 1
1003 Fish-Mad 27 SG SG +65-67456632 $3000 3
1004 Vale-Kiss 46 LD ENG +44-20-87634321 $6280 3
用awk實作從第二行開始列印每一行的第一列和第二列的功能。
[root@159 shell]# awk '{a[$1]=$2}END{for(i in a)print a[i]}' fl
Huang-Yu
Fish-Mad
Vale-Kiss
Name
Steven
數組a是關聯數組,a關聯到第一列,并将第二列的值賦給a。
這樣沒有按照原來的$2順序輸出。
[root@159 shell]# awk '{a[i]=$1;b[$1]=$2;i++}END{for(x=1;x<i;x++)print a[x],b[a[x]]}' fl
1001 Steven
1002 Huang-Yu
1003 Fish-Mad
1004 Vale-Kiss
注:i從0開始取值,而x從1開始。a為順序數組,b為關聯數組。