我正在从文本文件中提取信息,特别是“FEW080”
但是,在运行我的脚本时,这是我收到的错误 Value Too Great for Base (错误标记为“080”,因为有一个前导零,我假设它将它视为八进制,但我想抑制任何前导零
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
Run Code Online (Sandbox Code Playgroud)
在 bash 中,您可以在算术扩展中指定基数。
另外,正如您已经在使用的grep -o那样,您应该使用grep -oP后视和前视:
#!/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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
105 次 |
| 最近记录: |