Bash scripting help! –

Bash scripting help! –

Programming –

Hi experts, I have a bash script that needs to read a single line METAR from a file named: metar.txt

The script output (to the shell screen) should include:

1. the report type: either METAR or SPECI for “Special Report”

2. the station identifies (four characters starting with a K)

3. Day of the month and time the report was filed

4. Wind direction and speed, wind gusting, and variable wind directions

5. If the token ‘AUTO’ is present, you should output “This is a fully automated report”

6. If the token ‘COR’ is present, you should output “This is a corrected observation”

7. Visibility in statute miles

8. Light, medium, or heavy rain (-RA|RA|+RA)

9. Cloud Conditions: [0 or more occurrences of each of the following]

a. Sky Clear

b. Few Clouds <altitude>

c. Scattered Clouds <altitude>

d. Broken Clouds <altitude>

e. Overcast Clouds <altitude>

10. Temperature

11. Dew Point

12. Barometric Pressure

13. If the token ‘TH’ is present, you should output “Thunderstorms reported in the area”


Here is an example of a decoded METAR from a single line metar.txt file containing:

• METAR KBWI 031754Z 29008KT 250V320 10SM FEW023 SCT041 BKN080 BKN140 13/07 A3003 RMK AO2 SLP170 60001 T01280067 10133 20072 58002

o Report type: METAR

o Location: KBWI

o Day of month: 03

o Time: 17:54 UTC

o Wind: True direction = 290 degrees, Speed: 8 knots

o Wind direction is variable between 250 and 320

o Visibility: 10 Statute Miles

o Clouds: A few , at 2300 feet above sea level

o Clouds: Scattered , at 4100 feet above sea level

o Clouds: Broken clouds , at 8000 feet above sea level

o Clouds: Broken clouds , at 14000 feet above sea level

o Temperature: 13 degrees Celsius

o Dew point: 07 degrees Celsius

o Altimeter: 30.03 inHg


I have already done the first 4 of the outputs. Need the rest please!

Here is what I have so far:

# Number 1 … extract and print report type …
printf “Report type: “
egrep -o ‘METAR|SPECI’ metar.txt
# Number 2 … extract and print station information …
printf “Station: “
egrep -o ‘sK[A-Z]{3}s’ metar.txt
# Number 3a … extract and print date-time information …
day_of_month=$(cat metar.txt | cut -c12-13)
metar_time=$(cat metar.txt | cut -c14-17)
printf “nDay of the month: %dn” $day_of_month
printf “Time: %d Zulun” $metar_time
# Number 6 … extract wind speed and direction information …
wind_dir=$(egrep -o ‘s[0-9]{5}(G[0-9]{2})?KTs’ metar.txt | cut -c2-4)
wind_spd=$(egrep -o ‘s[0-9]{5}(G[0-9]{2})?KTs’ metar.txt | cut -c5-6)
printf “nWinds are from %s degrees at %s knotsn” $wind_dir $wind_spd
# Number 6a … if 7th character is a ‘G’, print gust info
gust_flag=$(egrep -o ‘s[0-9]{5}(G[0-9]{2})?KTs’ metar.txt | cut -c7-7)
if [ “$gust_flag” = “G” ]; then
gust_spd=$(egrep -o ‘s[0-9]{5}G?[0-9]?[0-9]?KTs’ metar.txt | cut -c8-9)
printf “Winds are gusting to %s knotsn” $gust_spd
# … if metar contains a variable winds token, print direction info
var_winds_flag=$(egrep -o ‘s[0-9]{3}V[0-9]{3}s’ metar.txt | cut -c5-5)
if [ “$var_winds_flag” = “V” ]; then
winds1=$(egrep -o ‘s[0-9]{3}V[0-9]{3}s’ metar.txt | cut -c2-4)
winds2=$(egrep -o ‘s[0-9]{3}V[0-9]{3}s’ metar.txt | cut -c6-8)
printf “Winds are variable from %s to %s degrees C.n” $winds1 $winds2