Bash egrep 混淆?

huangapple go评论47阅读模式
英文:

Bash egrep confusion?

问题

目前正在尝试编写一个Bash脚本,该脚本将搜索我的metar.txt文件中的变量并显示它们。我不太确定我现在在做什么。大部分脚本都是由我的老师预先制作的示例(他从未真正解释过大部分内容,或者我们没有关于它的信息)。到目前为止,我所做的是visib=$( egrep -o '\s[0-9]{2}\s' metar.txt | cut -c2-3)这一行。我认为我走在正确的道路上,但我一直得到一个输出为0。我想要从metar.txt中的1SM中提取1作为结果。我不太确定接下来该怎么做,是语法有问题还是我写错了。我正在寻求任何帮助或指向正确方向的建议。谢谢。

在我的metar.txt文件中的文本:

METAR KABC 121755Z AUTO 21016G24KT 180V240 1SM R11/P6000FT -RA BR BKN015 0VC025 06/04 A2990

Bash脚本:

#!/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

当前的可见度输出为0

我的输出一直是0,我试图让egrep命令从metar.txt文件中提取1SM中的1。

英文:

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.

答案1

得分: 0

请标记为"已回答"如果这回答了您的问题。谢谢!

编辑 - 我查了一下规范,意识到SM可能有更多的数字,所以我修复它以允许更多。

这应该有所帮助:

visib=$(egrep -o 's[0-9]*SM' metar.txt)

# 这会去掉末尾的"SM"部分
visib="${visib//SM}"

# 打印它...
printf "\nVisibility: %d Statute Miles\n" $visib

输出:

# 用内容填充文件
$ echo "METAR KABC 121755Z AUTO 21016G24KT 180V240 1SM R11/P6000FT -RA BR BKN015 0VC025 06/04 A2990" > metar.txt

# 提取可见度部分
$ visib=$(egrep -o 's[0-9]*SM' metar.txt)

# 去掉单位
visib=${visib//SM}

# 打印结果
$ printf "\nVisibility: %d Statute Miles\n" "$visib"

Visibility: 1 Statute Miles

您也可以像这样删除单位:

$ 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是"translate"的缩写,-d表示删除提供的字符(查看man tr获取更多信息)。

英文:

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).

huangapple
  • 本文由 发表于 2023年3月9日 12:31:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75680452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定