- 论坛徽章:
- 1
|
[ 本帖最后由 helloclei 于 2015-09-15 10:31 编辑 ]
正在看《awk与sed第三版》的电子版,第二章最后有一个关于州排序的问题,如下:
[backcolor=Yellow][root@localhost shell]# cat list
John Daggett, 341 King Road, Plymouth MA
Alice Ford, 22 East Broadway, Richmond VA
Orville Thomas, 11345 Oak Bridge Road, Tulsa OK
Terry Kalkas, 402 Lans Road, Beaver Falls PA
Eric Adams, 20 Post Road, Sudbury MA
Hubert Sims, 328A Brook Road, Roanoke VA
Amy Wilde, 334 Bayshore Pkwy, Mountain View CA
Sal Carpenter, 73 6th Street, Boston MA[/backcolor]
nameState脚本内容如下:
[backcolor=Yellow][root@localhost shell]# cat nameState
s/MA/, Masschusetts/
s/VA/, Virginia/
s/OK/, Oklahoma/
s/PA/, Pennsylvania/
s/CA/, California/[/backcolor]
byState内容如下:
[backcolor=Yellow][root@localhost shell]# cat byState
#!/bin/bash
awk -F, '{
print $4", "$0
}' $*|
sort|
awk -F, '
$1 == LastState {
print "\t" $2
}
$1 != LastState {
LastState = $1
print $1
print "\t" $2
}'[/backcolor]
执行的结果如下:
[backcolor=Yellow][root@localhost shell]# sed -f nameState list| bash byState
California
Amy Wilde
Masschusetts
Eric Adams
John Daggett
Sal Carpenter
Oklahoma
Orville Thomas
Pennsylvania
Terry Kalkas
Virginia
Alice Ford
Hubert Sims[/backcolor]
问题:
请问 $1 == LastState 这个判断是怎么定义的?书里说LastState是指上一个记录的州名,但是前面没有定义过这个变量,awk程序怎么会知道它就是上一条记录的州名呢? |
|