1. 从 <em>绿色呼吸</em> 获取当前时间监测数据,写入pm25.txt文件
reing@reing-Lenovo-Y430P:~/桌面$ curl http://www.pm25.com/city/beijing.html > pm25.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 212k 0 212k 0 0 596k 0 --:--:-- --:--:-- --:--:-- 597k
pm25.txt中各监测站信息由<li></li>
标签维护,每组<li></li>
中包含<span>
<a>
..等标签。
//pm25.txt 部分内容
<a class="pjadt_location" href="/city/mon/aqi/北京/万寿西宫.html" title="查看万寿西宫详细数据">万寿西宫</a>
<span class="pjadt_aqi">121<i class=""></i></span>
<span class="pjadt_quality"><em class="pjadt_quality_bglevel_3">轻度污染</em></span>
<a class="pjadt_wuranwu" href="/news/387.html" title="颗粒物(PM10)" target="_blank">颗粒物(PM10)</a>
<span class="pjadt_pm25">3 <em>μg/m³</em><i class=""></i></span>
<span class="pjadt_pm10">192 <em>μg/m³</em><i class=""></i></span>
<div class="clear"></div>
</li>
<li class="pj_area_data_item pj_area_data_item_darkbg">
<b></b>
<a class="pjadt_location" href="/city/mon/aqi/北京/定陵.html" title="查看定陵详细数据">定陵</a>
<span class="pjadt_aqi">26<i class=""></i></span>
<span class="pjadt_quality"><em class="pjadt_quality_bglevel_1">优</em></span>
<a class="pjadt_wuranwu" href="javascript:void(0)" title="—" target="_blank">—</a>
<span class="pjadt_pm25">3 <em>μg/m³</em><i class=""></i></span>
<span class="pjadt_pm10">5 <em>μg/m³</em><i class=""></i></span>
<div class="clear"></div>
</li>
<li class="pj_area_data_item pj_area_data_item_darkbg">
<b></b>
<a class="pjadt_location" href="/city/mon/aqi/北京/东四.html" title="查看东四详细数据">东四</a>
2. 使用tr
命令删除所有换行,写入new.txt
reing@reing-Lenovo-Y430P:~/桌面$ tr "\n" " " < pm25.txt > new.txt
pm25.txt中内含标签之间有换行符,不删除的话监测点信息分布在不同行,不易使用awk命令处理,因此使用tr命令删除所有换行
//new.txt部分内容
li class="pj_area_data_item pj_area_data_item_darkbg"> <b></b> <a class="pjadt_location" href="/city/mon/aqi/北京/万寿西宫.html" title="查看万寿西宫详细数据">万寿西宫</a> <span class="pjadt_aqi">121<i class=""></i></span> <span class="pjadt_quality"><em class="pjadt_quality_bglevel_3">轻度污染</em></span> <a class="pjadt_wuranwu" href="/news/387.html" title="颗粒物(PM10)" target="_blank">颗粒物(PM10)</a> <span class="pjadt_pm25">3 <em>μg/m³</em><i class=""></i></span> <span class="pjadt_pm10">192 <em>μg/m³</em><i class=""></i></span> <div class="clear"></div> </li> <li class="pj_area_data_item pj_area_data_item_darkbg"> <b></b> <a class="pjadt_location" href="/city/mon/aqi/北京/定陵.html" title="查看定陵详细数据">定陵</a> <span class="pjadt_aqi">26<i class=""></i></span> <span class="pjadt_quality"><em class="pjadt_quality_bglevel_1">优</em></span> <a class="pjadt_wuranwu" href="javascript:void(0)" title="—" target="_blank">—</a> <span class="pjadt_pm25">3 <em>μg/m³</em><i class=""></i></span> <span class="pjadt_pm10">5 <em>μg/m³</em><i class=""></i></span> <div class="clear"></div> </li>
3. 使用sed
命令为特定行前添加换行,写入new2.txt
reing@reing-Lenovo-Y430P:~/桌面$ sed -e 's/<li/\n<li/g' -e 's/更新时间:/\n更新时 间: /g' new.txt > new2.txt
各个监测点之间,监测点和冗余信息之间需要换行区分,否则信息集中在同一行。同时要为保证取到更新时间,需要对更新时间前添加换行,并在“更新时间:”后添加空格以分离“更新时间:”和具体日期
//new2.txt部分内容
<li class="pj_area_data_item pj_area_data_item_darkbg"> <b></b> <a class="pjadt_location" href="/city/mon/aqi/北京/万寿西宫.html" title="查看万寿西宫详细数据">万寿西宫</a> <span class="pjadt_aqi">121<i class=""></i></span> <span class="pjadt_quality"><em class="pjadt_quality_bglevel_3">轻度污染</em></span> <a class="pjadt_wuranwu" href="/news/387.html" title="颗粒物(PM10)" target="_blank">颗粒物(PM10)</a> <span class="pjadt_pm25">3 <em>μg/m³</em><i class=""></i></span> <span class="pjadt_pm10">192 <em>μg/m³</em><i class=""></i></span> <div class="clear"></div> </li>
<li class="pj_area_data_item pj_area_data_item_darkbg"> <b></b> <a class="pjadt_location" href="/city/mon/aqi/北京/定陵.html" title="查看定陵详细数据">定陵</a> <span class="pjadt_aqi">26<i class=""></i></span> <span class="pjadt_quality"><em class="pjadt_quality_bglevel_1">优</em></span> <a class="pjadt_wuranwu" href="javascript:void(0)" title="—" target="_blank">—</a> <span class="pjadt_pm25">3 <em>μg/m³</em><i class=""></i></span> <span class="pjadt_pm10">5 <em>μg/m³</em><i class=""></i></span> <div class="clear"></div> </li>
4. 使用sed
命令将标签替换为空格,写入new3.txt
reing@reing-Lenovo-Y430P:~/桌面$ sed 's/<[^>]*>/ /g' new2.txt > new3.txt
目的在于删除所有标签,只保留下显示信息,但要保证信息之间有至少一个空格,因此将标签换为空格,作为信息的划分
//new3.txt部分内容
万寿西宫 121 轻度污染 颗粒物(PM10) 3 μg/m³ 192 μg/m³
定陵 26 优 — 3 μg/m³ 5 μg/m³
5. 使用awk
命令提取更新时间
reing@reing-Lenovo-Y430P:~/桌面$ date='awk '/更新时间/{print $2}' new3.txt'
reing@reing-Lenovo-Y430P:~/桌面$ time=`awk '/更新时间/{print $3}' new3.txt`
reing@reing-Lenovo-Y430P:~/桌面$ export date
reing@reing-Lenovo-Y430P:~/桌面$ export time
//通过awk命令提取到更新时间的日期和时刻,添加到环境变量,以便在后续输出时,awk变量能够访问shell变量
6. 使用awk
命令提取各地点的信息,同时间写入new4.txt
reing@reing-Lenovo-Y430P:~/桌面$ awk '/细颗粒物\(PM2\.5\)/{printf "%s ", ENVIRON["date"];printf "%s,", ENVIRON["time"];printf "%s,%s\n",$1,$2}' < new3.txt > new4.txt
//匹配“细颗粒物(PM2.5)”找到信息所在行,提取监测点名称和污染指数,连同更新时间一并写入最终文件
结果
//new4.txt内容
2017-03-29 12:00,东四,214
2017-03-29 12:00,农展馆,211
2017-03-29 12:00,官园,150
2017-03-29 12:00,海淀区万柳,189
2017-03-29 12:00,顺义新城,215
2017-03-29 12:00,怀柔镇,205
2017-03-29 12:00,昌平镇,158
2017-03-29 12:00,奥体中心,198
2017-03-29 12:00,古城,214
2017-03-29 12:00,东四,214
2017-03-29 12:00,农展馆,211
2017-03-29 12:00,官园,179
2017-03-29 12:00,海淀区万柳,195
2017-03-29 12:00,顺义新城,215
2017-03-29 12:00,怀柔镇,205
2017-03-29 12:00,昌平镇,182
2017-03-29 12:00,奥体中心,198
2017-03-29 12:00,古城,214
网友评论