首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在正则表达式脚本中使用If/Else

在正则表达式脚本中使用If/Else
EN

Stack Overflow用户
提问于 2016-10-06 02:24:18
回答 1查看 53关注 0票数 0

我想写点什么来读报告的部分内容。我以为我知道如何在其中使用if/ keep逻辑,但我总是会出错。

代码:

代码语言:javascript
复制
printf "The report type is: "
egrep -o 'METAR|SPECI' metar1.txt

printf "Station: "
egrep -o 'K[A-Z]{3}' metar1.txt

printf "Day of the month: "
date_time=$(egrep -o '[0-9]{6}Z' metar1.txt)
echo "$date_time"|cut -c1-2
printf "Time of day: "
echo "$date_time"|cut -c3-6

if [[ egrep 'AUTO' metar1.txt ]];
then
    echo "This is a fully automated report."
elif [[ egrep -o 'COR' metar1.txt ]];
then
    echo "This is a corrected observation."
else
    echo "There is neither 'AUTO' or 'COR' in this report."
fi

wind_degree=$(egrep -o '\s[0-9]{5}G?[0-9]?[0-9]?KT\s' metar1.txt|cut -c2-4)
wind_speed=$(egrep -o '\s[0-9]{5}G?[0-9]?[0-9]?KT\s' metar1.txt|cut -c5-6)
printf "Winds are from $wind_degree degree at $wind_speed knots\n"

我在第13行看到了错误:

代码语言:javascript
复制
line 13: conditional binary operator expected, 
line 13: syntax error near `'AUTO'', 
line 13: `if [[ egrep 'AUTO' metar1.txt ]];'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-06 03:02:18

if ... fi块替换为:

代码语言:javascript
复制
if    egrep 'AUTO'   metar1.txt ;  then
    echo "This is a fully automated report."
elif  egrep -o 'COR' metar1.txt ; then
    echo "This is a corrected observation."
else
    echo "There is neither 'AUTO' or 'COR' in this report."
fi
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39886530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档