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

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

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

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

echo " DOWNLOAD PM_2_5 INFO FINISHED"

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

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

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

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

_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

你可能感兴趣的:(杂学,linux,shell,grep)