awk排序作业

时间:2021-03-22 10:11:47

输入:给定一个hotelinfo文件,文件格式如下:

shanghai_city_7208      上海全季酒店淮海路店

shanghai_city_14744     锦江之星上海金山城市沙滩店

jinan_2794      章丘市大众旅馆

carmel_ca_5     Carmel River Inn

格式说明:

1. 一共两列,之间使用tab分隔

2. 第一列是酒店代号,第二列是酒店名称

3. 以shanghai_city_7208为例,前面的shanghai_city代表城市

要求如下:

1. 输出一个文件,和hotelinfo格式一样,但是按照酒店代号进行降序排序

2. 输出一个文件,两列,第一列是城市代号,第二列是这个城市下的酒店数,但是按照酒店数进行降序排序

文件如下:

hotelinfo.txt

1.先表示我看错题目了,我以为是按照第一列的最后的数字排序,瞬间复杂度增加,于是我写了一段蛋疼的awk程序。

首先对文本进行排序处理,awk程序为hotelsort.awk

BEGIN{FS="_";ORS="";content=""}
#将原来的文件按照“_”切分,确保酒店的数字代码在空格的第二列
{for(i=;i<(NF-);i++) content=content""$i"_";content=content""$(NF-)" "$NF"\n"}
#用sort命令对酒店代码排序
END{printf("%s", content) | "sort -rn -k 2 > hotelsorted.txt"}

执行

awk -f hotelsort.awk hotelinfo.txt

此时hotelsorted.txt如下

falling_waters_wv  Holiday Inn Express MARTINSBURG-NORTH
fairlee_vt Silver Maple Lodge & Cottages
etroubles_ita Hotel Beau Sejour
escanaba_mi Comfort Suites Escanaba
elk_river_mn Country Inn & Suites By Carlson Elk River
eckelsheim_ger Hotel Klosterhof Eckelsheim
dundalk_il 邓多克皇冠假日酒店
delwara_ind Devi Garh
deidesheim_ger Steigenberger Hotel Deidesheim
colton_ca Holiday Inn Express Colton
chita Arkadia
burbank_ca Burbank-Media Center

最后格式化一下这个文件

awk '{print $1"_"$2" "$3}' hotelsorted.txt > hotelsortedformat.txt

得到最后结果

fancy_gap_va_1 Days
falling_waters_wv_1 Holiday
fairlee_vt_1 Silver
etroubles_ita_1 Hotel
escanaba_mi_1 Comfort
elk_river_mn_1 Country
eckelsheim_ger_1 Hotel
dundalk_il_1 邓多克皇冠假日酒店
delwara_ind_1 Devi
deidesheim_ger_1 Steigenberger
colton_ca_1 Holiday
chita_1 Arkadia
burbank_ca_1 Burbank-Media

收工

2. 跟第一题差不多,写个awk  hotelsum.awk

BEGIN{FS="_"}
#跟第一题类似,用数组统计个数
{citycode="";for(i=;i<(NF-);i++) citycode=citycode""$i"_";if(NF>)citycode=citycode""$(NF-);sum[citycode]++}
END{for(city in sum) printf("%s\t%4d\n", city, sum[city])}

然后调用一下并排序

awk -f hotelsum.awk hotelinfo.txt | sort -t " " -k  -nr

结果:

torquay_vic
tel_aviv
rovinj_cro
torremolinos_esp
the_hague
torquay_uk
paris_city
telford_uk
london_england
los_angeles_usa
fairview_pa
tarragona_esp
beijing_city
tampere_fi
shanghai_city
san_antonio_usa
suzhou_jiangsu
manchester_usa
tossa_de_mar_spa
timmendorfer_strand
terracina_ita
riccione_ita
chongqing_city
yichun_jiangxi
west_memphis_ar
sydney_australia
rovaniemi_fi
dover_de
anderson_sc
ubud_id
taunton_ma
taizhou_zhejiang
robinson_il
okehampton_uk
melbourne_australia
las_vegas
koh_phuket_tha
harrisburg_pa
hamilton_on
gardner_ks
faulkton_sd
beppu_jp
west_columbia_tx
volos_gre
thunder_bay_on
the_entrance_nsw
teignmouth_uk
pittsfield_il
newcastle_united_kingdom
motta_camastra
montvale_nj
montauk_ny
lindos_gre
liberal_ks
lakeside_ca
fort_washington_pa
fillmore_ut
escanaba_mi
birmingham_usa
big_island
zanzibar_tza
yulin_shanxi_02
yulin_guangxi
ypsilanti_mi
yelverton_uk
wigston_uk
wangerooge_ger
vysoke_tatry_sk
trento_ita
the_rocks_nsw
tamworth_uk
stadtoldendorf_ger
somerset_uk
singapore_city
shipley_uk
shenandoah_tx
schmallenberg_ger
santa_elena_gt
san_diego
salado_tx
saint_petersburg
rock_port_mo
riverside_il
richmond_vt
ravenna_italy
prestwick_uk
piste_mex
peoria_az
pensacola_fl
painesville_oh
osnabruck_ger
new_york_city
new_delhi
nelson_bay
montclair_nj
montclair_ca
mexico_city
menlo_park_ca
mattoon_il
mason_wv
madonna_di_campiglio
lone_tree_co
london_England
livingston_nj
lincoln_uk
lakeside_az
krumbach_ger
kota_kinabalu
koh_samui
kernersville_nc
kansas_city
jackson_ms
huizhou_guangdong
huelva_esp
hongkong_city
himeji_jp
hillsborough_nc
hille_ger
hershey_pa
havelock_north_nz
hakodate_jp
grand_rapids
glendale_ca
glen_allen_va
galena_park_tx
fuzhou_fujian
ft_lauderdale
french_lick_in
frazer_pa
fondi_ita
fancy_gap_va
falling_waters_wv
fairport_ny
fairlee_vt
etroubles_ita
elk_river_mn
eckelsheim_ger
dundalk_il
denpasar_bali
delwara_ind
deidesheim_ger
davie_fl
covington_kt
corpus_christi_tx
colton_ca
cefalu_ita
cavriglia_ita
carmel_ca
campinas_bra
burbank_ca
budapest_city
*lyn_center_mn
bratislava_city
aberdeen_united_kingdom