欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[SHELL SCRIPT] LINUX正则表达式 课堂作业 获取PM2.5天气信息

程序员文章站 2022-07-14 15:43:02
...

因特网,你好强大![SHELL SCRIPT] LINUX正则表达式 课堂作业 获取PM2.5天气信息

#!/bin/bash
rm result.txt
rm beijing*

wget http://www.pm25s.com/beijing.html
if [ $? -ne 0 ]; then
	exit 1
fi

echo "<INFO> DOWNLOAD PM_2_5 INFO FINISHED"

DATE=$(grep -oP "(?<=数据更新时间:).*(?=</div>)" ./beijing.html)

echo "<INFO> DATE MESSAGE: ${DATE}"

SITE_LIST_T=$(grep -oP '<span class=\"site\">[^0-9^/]*</span>' ./beijing.html |
			  grep -oP '(?<=<span class=\"site\">).*(?=</span>)')

PM_2_5_LIST_T=$(grep -oP '<span class=\"aqis\">[0-9\-\.]*</span>' ./beijing.html |
			  	grep -oP '(?<=<span class=\"aqis\">).*(?=</span>)')

_site_size=0
_pm_size=0

for SITE in $SITE_LIST_T
do
	SITE_LIST[$_site_size]=$SITE
	let "_site_size+=1"
done

for PM in $PM_2_5_LIST_T
do
	PM_2_5_LIST[$_pm_size]=$PM
	let "_pm_size+=1"
done

echo ""
echo "PM_LIST_LENGTH: ${_pm_size} SITE_LIST_LENGTH: ${_site_size}"
echo ""

_site=0
for ((_index=0; _index<${_pm_size};_index+=7))
do
	echo "${DATE} ${SITE_LIST[${_site}]} ${PM_2_5_LIST[${_index}]}" >> result.txt
	let "_site+=1"


	if [ $_site -eq ${_site_size} ]; then
		break
	fi
done

cat result.txt