-1

I am extracting information from a text file specifically "FEW080"

However, when running my script this is the error I receive Value too Great for Base (error token is "080" Since there is a leading zero I am assuming it is taking it as octal but I want to suppress any leading zeros

fewClouds=$( egrep -o '\sFEW[0-9]{3}\s' metar.txt | cut -c5-7 ) 

if [ -n "$fewClouds" ]; then
fewClouds=$(($( egrep -o '\sFEW[0-9]{3}\s' metar.txt | cut -c5-7) *100))

printf "\nFew Clouds at %s feet" $fewClouds
fi
tembo
  • 1
  • 1
  • Does https://stackoverflow.com/questions/24777597/value-too-great-for-base-error-token-is-08 answer your question? – KamilCuk Oct 03 '22 at 21:23
  • Not exactly sure how I would implement that solution within my code, tried varying ways still doesn't seem to work – tembo Oct 03 '22 at 21:37

1 Answers1

5

In bash you can specify a base in the arithmetic expansions.

Also, as you're already using grep -o then you should use grep -oP with a look-behind and a look-ahead:

#!/bin/bash

if fewClouds=$(grep -oP '(?<=\sFEW)[0-9]{3}(?=\s)' metar.txt)
then
    fewClouds=$(( 10#$fewClouds * 100 ))

    printf "\nFew Clouds at %s feet" "$fewClouds"
fi
Fravadona
  • 13,917
  • 1
  • 23
  • 35
  • 1
    Note that not all versions of `grep` having `-o` are guaranteed to also have `-P` -- linking libpcre is a separate compile-time option in GNU grep. – Charles Duffy Oct 03 '22 at 21:34
  • Thanks @CharlesDuffy. Now that you said it, it does make sense that pcre support is optional – Fravadona Oct 03 '22 at 21:37