#!/bin/bash file=`ls kadai2_data/kadai2_data.csv` #変数fileにディレクトリkadai2_dataの中にあるkadai2_data.csvを代入する。 judge=0 while [[ $judge = 0 ]] do echo "県名を入力してください。" read prefecture echo "市町村名を入力してください。" read city awk -F'&' '{print $1","$2}' $file | awk -F',' -v prefecture=$prefecture -v city=$city '{if($1==prefecture && $2==city){print"光害指数は\n"$5;exit;}}' #&で区切られた都道府県と市町村をコンマで区切ることで、一列目が都道府県になり、二列目が市町村になる。変数prefectureと変数cityが一列目と二列目に一致する行の五列目(公害指数)を表示する。 judge=`cut -d , -f 1 $file | grep -c -w "$prefecture&$city"` #コンマで区切られたデータの一列目を抽出し変数prefecture&変数cityに一致する行数を変数judgeに代入する。同一の地名は一つしかないので一致すれば1、そうでなければ0が変数judgeに代入される。 judgepref=`awk -F'&' '{print $1","$2}' $file | cut -d , -f 1 | grep -c -w "$prefecture"` #変数prefectureと一致する都道府県名がある行数を変数judgeprefに代入する。 judgecity=`awk -F'&' '{print $1","$2}' $file | cut -d , -f 2 | grep -c -w "$city"` #変数cityと一致する市町村名がある行数を変数judgecityに代入する。 if [[ $judgepref = 0 ]] ; then echo "存在しない都道府県名です。" fi if [[ $judgecity = 0 ]] ; then echo "存在しない市町村名です。" fi if [[ $judge = 0 && $judgepref > 0 && $judgecity > 0 ]] ; then echo "名前が違います。" fi done Latitude=`grep -w "$prefecture&$city" $file | cut -d , -f 2` #変数Latitudeには入力された場所の緯度が代入される。 Longitude=`grep -w "$prefecture&$city" $file | cut -d , -f 3` #上と同様で経度が代入される。 echo "半径100km 以内にある光害指数が低い場所は" awk -F"," -v Latitude=$Latitude -v Longitude=$Longitude '{if((((Latitude-$2)*30.82*3600)^2+((Longitude-$3)*25.11*3600)^2)^0.5 <= 100000){print $1,$4}}' $file | sort -k 2,2n -t" " | awk 'NR==1,NR==3{print$0}' #変数Latitude、Longtitudeとそれぞれの行の二列目、三列目をもちいて入力された場所の半径100km以内にある場所の行を取り出す。その1列目(場所)、四列目(公害指数)を抽出し、それを公害指数の小さい順に並び変え、上位三行を画面に表示する。