输入:给定一个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=1;i<(NF-1);i++) content=content""$i"_";content=content""$(NF-1)" "$NF"\n"}
#用sort命令对酒店代码排序 END{printf("%s", content) | "sort -rn -k 2 > hotelsorted.txt"}
执行
awk -f hotelsort.awk hotelinfo.txt
此时hotelsorted.txt如下
falling_waters_wv 1 Holiday Inn Express MARTINSBURG-NORTH fairlee_vt 1 Silver Maple Lodge & Cottages etroubles_ita 1 Hotel Beau Sejour escanaba_mi 1 Comfort Suites Escanaba elk_river_mn 1 Country Inn & Suites By Carlson Elk River eckelsheim_ger 1 Hotel Klosterhof Eckelsheim dundalk_il 1 邓多克皇冠假日酒店 delwara_ind 1 Devi Garh deidesheim_ger 1 Steigenberger Hotel Deidesheim colton_ca 1 Holiday Inn Express Colton chita 1 Arkadia burbank_ca 1 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=1;i<(NF-1);i++) citycode=citycode""$i"_";if(NF>2)citycode=citycode""$(NF-1);sum[citycode]++} END{for(city in sum) printf("%s\t%4d\n", city, sum[city])}
然后调用一下并排序
awk -f hotelsum.awk hotelinfo.txt | sort -t " " -k 2 -nr
结果:
torquay_vic 43 tel_aviv 33 rovinj_cro 30 torremolinos_esp 29 the_hague 17 torquay_uk 16 paris_city 15 telford_uk 13 london_england 11 los_angeles_usa 9 fairview_pa 9 tarragona_esp 8 beijing_city 8 tampere_fi 7 shanghai_city 7 san_antonio_usa 7 suzhou_jiangsu 6 manchester_usa 6 tossa_de_mar_spa 5 timmendorfer_strand 5 terracina_ita 5 riccione_ita 5 chongqing_city 5 yichun_jiangxi 4 west_memphis_ar 4 sydney_australia 4 rovaniemi_fi 4 dover_de 4 anderson_sc 4 ubud_id 3 taunton_ma 3 taizhou_zhejiang 3 robinson_il 3 okehampton_uk 3 melbourne_australia 3 las_vegas 3 koh_phuket_tha 3 harrisburg_pa 3 hamilton_on 3 gardner_ks 3 faulkton_sd 3 beppu_jp 3 west_columbia_tx 2 volos_gre 2 thunder_bay_on 2 the_entrance_nsw 2 teignmouth_uk 2 pittsfield_il 2 newcastle_united_kingdom 2 motta_camastra 2 montvale_nj 2 montauk_ny 2 lindos_gre 2 liberal_ks 2 lakeside_ca 2 fort_washington_pa 2 fillmore_ut 2 escanaba_mi 2 birmingham_usa 2 big_island 2 zanzibar_tza 1 yulin_shanxi_02 1 yulin_guangxi 1 ypsilanti_mi 1 yelverton_uk 1 wigston_uk 1 wangerooge_ger 1 vysoke_tatry_sk 1 trento_ita 1 the_rocks_nsw 1 tamworth_uk 1 stadtoldendorf_ger 1 somerset_uk 1 singapore_city 1 shipley_uk 1 shenandoah_tx 1 schmallenberg_ger 1 santa_elena_gt 1 san_diego 1 salado_tx 1 saint_petersburg 1 rock_port_mo 1 riverside_il 1 richmond_vt 1 ravenna_italy 1 prestwick_uk 1 piste_mex 1 peoria_az 1 pensacola_fl 1 painesville_oh 1 osnabruck_ger 1 new_york_city 1 new_delhi 1 nelson_bay 1 montclair_nj 1 montclair_ca 1 mexico_city 1 menlo_park_ca 1 mattoon_il 1 mason_wv 1 madonna_di_campiglio 1 lone_tree_co 1 london_England 1 livingston_nj 1 lincoln_uk 1 lakeside_az 1 krumbach_ger 1 kota_kinabalu 1 koh_samui 1 kernersville_nc 1 kansas_city 1 jackson_ms 1 huizhou_guangdong 1 huelva_esp 1 hongkong_city 1 himeji_jp 1 hillsborough_nc 1 hille_ger 1 hershey_pa 1 havelock_north_nz 1 hakodate_jp 1 grand_rapids 1 glendale_ca 1 glen_allen_va 1 galena_park_tx 1 fuzhou_fujian 1 ft_lauderdale 1 french_lick_in 1 frazer_pa 1 fondi_ita 1 fancy_gap_va 1 falling_waters_wv 1 fairport_ny 1 fairlee_vt 1 etroubles_ita 1 elk_river_mn 1 eckelsheim_ger 1 dundalk_il 1 denpasar_bali 1 delwara_ind 1 deidesheim_ger 1 davie_fl 1 covington_kt 1 corpus_christi_tx 1 colton_ca 1 cefalu_ita 1 cavriglia_ita 1 carmel_ca 1 campinas_bra 1 burbank_ca 1 budapest_city 1 brooklyn_center_mn 1 bratislava_city 1 aberdeen_united_kingdom 1