Issue
Currently trying to write a bash script that will search my metar.txt file for the variables and display them. I'm not really sure what I am doing currently. Most of all the script was a sample pre-made by my teacher (he really never explained most of this or we have info on it). So far what I've done though is the visib=$( egrep -o '\s[0-9]{2}\s' metar.txt | cut -c2-3)
line. I think, I'm on the right path but I keep getting a output for 0. While I'm trying to get the 1SM in the metar.txt to pull the 1 as the result. I'm not really sure what to do next, if its my syntax or I have written it wrong. Any help or a point in the right direction is what, I'm looking for. Thanks.
Text in my metar.txt file
METAR KABC 121755Z AUTO 21016G24KT 180V240 1SM R11/P6000FT -RA BR BKN015 0VC025 06/04 A2990
bash plane script
#!/bin/bash
printf "Report type: "
egrep -o 'METAR|SPECI' metar.txt
printf "Location: "
egrep -o '\sK[A-Z]{3}\s' metar.txt
#
day=$( egrep -o '\s[0-9]{6}Z\s' metar.txt | cut -c2-3 )
hour=$( egrep -o '\s[0-9]{6}Z\s' metar.txt | cut -c4-5 )
minute=$( egrep -o '\s[0-9]{6}Z\s' metar.txt | cut -c6-7 )
printf "Day of the month: %d\n" $day
printf "Time: %d:%d Zulu" $hour $minute
#
windDir=$( egrep -o '\s[0-9]{5}(G[0-9]{2})?KT\s' metar.txt | cut -c2-4 )
windSpd=$( egrep -o '\s[0-9]{5}(G[0-9]{2})?KT\s' metar.txt | cut -c5-6 )
printf "\nWinds are from %d degrees at %d Knots" $windDir $windSpd
#
gustFlag=$( egrep -o '\s[0-9]{5}(G[0-9]{2})?KT\s' metar.txt | cut -c7-7 )
if [ "$gustFlag" = "G" ]; then
gustSpd=$( egrep -o '\s[0-9]{5}(G[0-9]{2})?KT\s' metar.txt | cut -c8-9 )
printf "\nWinds are gusting at %d Knots" $gustSpd
fi
#
varWinds=$( egrep -o '\s[0-9]{3}V[0-9]{3}\s' metar.txt )
if [ -n "$varWinds" ]; then
dir1=$( egrep -o '\s[0-9]{3}V[0-9]{3}\s' metar.txt | cut -c2-4)
dir2=$( egrep -o '\s[0-9]{3}V[0-9]{3}\s' metar.txt | cut -c6-8)
printf "\nWinds are variable from %s degrees to %s degrees" $dir1 $dir2
fi
#
visib=$( egrep -o '\s[0-9]{2}\s' metar.txt | cut -c2-3)
printf "\nVisibility: %d Statute Miles" $visib
Current Visibility outputing 0
My ouput keeps computing 0 and I'm trying to get the egrep command to pull the 1 for 1SM in the metar.txt file.
Solution
Please select answered if this answered your question. Thanks!
Edit - I looked up the spec and realized there could be more numbers for SM so I fixed it to allow more.
This should help:
visib=$(egrep -o '\s[0-9]*SM' metar.txt)
# this strips the "SM" part off the end
visib="${visib//SM}"
# print it...
printf "\nVisibility: %d Statute Miles\n" $visib
Output:
# populate file with content
$ echo "METAR KABC 121755Z AUTO 21016G24KT 180V240 1SM R11/P6000FT -RA BR BKN015 0VC025 06/04 A2990" > metar.txt
# extract the visibility part
$ visib=$(egrep -o '\s[0-9]*SM' metar.txt)
# strip off the units
visib=${visib//SM}
# print results
$ printf "\nVisibility: %d Statute Miles\n" "$visib"
Visibility: 1 Statute Miles
You can also do something like this to delete the units:
$ egrep -o '\s[0-9]*SM' metar.txt
13SM
$ egrep -o '\s[0-9]*SM' metar.txt | tr -d 'SM'
13
$ egrep -o '\s[0-9]*SM' metar.txt | tr -d '[A-Z]'
13
tr
is "translate" and -d
means delete the characters provided (type man tr
for more).
Answered By - TheAnalogyGuy Answer Checked By - Katrina (WPSolving Volunteer)