【sed & awk 第二版笔记】以州和人名排列_P38

[root@nhserver1 02]# cat list
John Daggett, 341 King Road, Plymouth MA
Alice Ford, 22 East Broadday, Richmond VA
Orville Thomas, 11345 Oak Brideg Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Raod, Sudbury MA
Hubert Sims, 328A * Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston MA

[root@nhserver1 02]# cat nameState
s/ MA/, Massachusetts/
s/ PA/, Pennsylvania/
s/ CA/, California/
s/ VA/, Virginai/
s/ OK/, Oklahoma/

[root@nhserver1 02]# cat byState
#! /bin/sh
awk -F, '{
        print $4 ", " $0
        }' $* |
sort |
awk -F, '
$1 == LastState {
        print "\t" $2
}
$1 != LastState {
        LastState = $1
        print $1
        print "\t" $2
}'

说明:

$* :所有参数列表,它将所有的引用变量视为一个整体。在此将它删除无大碍

sort :对州进行排序很关键,为下边的awk语句提供基础数据

LastState = $1:重要的赋值语句

$1 == LastState :对是否同州做判断

[root@nhserver1 02]# sed -f nameState list | ./byState
 California
         Amy Wilde
 Massachusetts
         Eric Adams
         John Daggett
         Sal Carpenter
 Oklahoma
         Orville Thomas
 Pennsylvania
         Terry Kalkas
 Virginai
         Alice Ford
         Hubert Sims

上一篇:Android基础知识03—Activity的基本用法


下一篇:Angularjs在线api文档