Bash egrep 混淆?

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

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文件中的文本:

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

Bash脚本:

  1. #!/bin/bash
  2. printf "Report type: "
  3. egrep -o 'METAR|SPECI' metar.txt
  4. printf "Location: "
  5. egrep -o '\sK[A-Z]{3}\s' metar.txt
  6. #
  7. day=$( egrep -o '\s[0-9]{6}Z\s' metar.txt | cut -c2-3 )
  8. hour=$( egrep -o '\s[0-9]{6}Z\s' metar.txt | cut -c4-5 )
  9. minute=$( egrep -o '\s[0-9]{6}Z\s' metar.txt | cut -c6-7 )
  10. printf "Day of the month: %d\n" $day
  11. printf "Time: %d:%d Zulu" $hour $minute
  12. #
  13. windDir=$( egrep -o '\s[0-9]{5}(G[0-9]{2})?KT\s' metar.txt | cut -c2-4 )
  14. windSpd=$( egrep -o '\s[0-9]{5}(G[0-9]{2})?KT\s' metar.txt | cut -c5-6 )
  15. printf "\nWinds are from %d degrees at %d Knots" $windDir $windSpd
  16. #
  17. gustFlag=$( egrep -o '\s[0-9]{5}(G[0-9]{2})?KT\s' metar.txt | cut -c7-7 )
  18. if [ "$gustFlag" = "G" ]; then
  19. gustSpd=$( egrep -o '\s[0-9]{5}(G[0-9]{2})?KT\s' metar.txt | cut -c8-9 )
  20. printf "\nWinds are gusting at %d Knots" $gustSpd
  21. fi
  22. #
  23. varWinds=$( egrep -o '\s[0-9]{3}V[0-9]{3}\s' metar.txt )
  24. if [ -n "$varWinds" ]; then
  25. dir1=$( egrep -o '\s[0-9]{3}V[0-9]{3}\s' metar.txt | cut -c2-4)
  26. dir2=$( egrep -o '\s[0-9]{3}V[0-9]{3}\s' metar.txt | cut -c6-8)
  27. printf "\nWinds are variable from %s degrees to %s degrees" $dir1 $dir2
  28. fi
  29. #
  30. visib=$( egrep -o '\s[0-9]{2}\s' metar.txt | cut -c2-3)
  31. 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

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

bash plane script

  1. #!/bin/bash
  2. printf "Report type: "
  3. egrep -o 'METAR|SPECI' metar.txt
  4. printf "Location: "
  5. egrep -o '\sK[A-Z]{3}\s' metar.txt
  6. #
  7. day=$( egrep -o '\s[0-9]{6}Z\s' metar.txt | cut -c2-3 )
  8. hour=$( egrep -o '\s[0-9]{6}Z\s' metar.txt | cut -c4-5 )
  9. minute=$( egrep -o '\s[0-9]{6}Z\s' metar.txt | cut -c6-7 )
  10. printf "Day of the month: %d\n" $day
  11. printf "Time: %d:%d Zulu" $hour $minute
  12. #
  13. windDir=$( egrep -o '\s[0-9]{5}(G[0-9]{2})?KT\s' metar.txt | cut -c2-4 )
  14. windSpd=$( egrep -o '\s[0-9]{5}(G[0-9]{2})?KT\s' metar.txt | cut -c5-6 )
  15. printf "\nWinds are from %d degrees at %d Knots" $windDir $windSpd
  16. #
  17. gustFlag=$( egrep -o '\s[0-9]{5}(G[0-9]{2})?KT\s' metar.txt | cut -c7-7 )
  18. if [ "$gustFlag" = "G" ]; then
  19. gustSpd=$( egrep -o '\s[0-9]{5}(G[0-9]{2})?KT\s' metar.txt | cut -c8-9 )
  20. printf "\nWinds are gusting at %d Knots" $gustSpd
  21. fi
  22. #
  23. varWinds=$( egrep -o '\s[0-9]{3}V[0-9]{3}\s' metar.txt )
  24. if [ -n "$varWinds" ]; then
  25. dir1=$( egrep -o '\s[0-9]{3}V[0-9]{3}\s' metar.txt | cut -c2-4)
  26. dir2=$( egrep -o '\s[0-9]{3}V[0-9]{3}\s' metar.txt | cut -c6-8)
  27. printf "\nWinds are variable from %s degrees to %s degrees" $dir1 $dir2
  28. fi
  29. #
  30. visib=$( egrep -o '\s[0-9]{2}\s' metar.txt | cut -c2-3)
  31. 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可能有更多的数字,所以我修复它以允许更多。

这应该有所帮助:

  1. visib=$(egrep -o 's[0-9]*SM' metar.txt)
  2. # 这会去掉末尾的"SM"部分
  3. visib="${visib//SM}"
  4. # 打印它...
  5. printf "\nVisibility: %d Statute Miles\n" $visib

输出:

  1. # 用内容填充文件
  2. $ echo "METAR KABC 121755Z AUTO 21016G24KT 180V240 1SM R11/P6000FT -RA BR BKN015 0VC025 06/04 A2990" > metar.txt
  3. # 提取可见度部分
  4. $ visib=$(egrep -o 's[0-9]*SM' metar.txt)
  5. # 去掉单位
  6. visib=${visib//SM}
  7. # 打印结果
  8. $ printf "\nVisibility: %d Statute Miles\n" "$visib"
  9. Visibility: 1 Statute Miles

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

  1. $ egrep -o 's[0-9]*SM' metar.txt
  2. 13SM
  3. $ egrep -o 's[0-9]*SM' metar.txt | tr -d 'SM'
  4. 13
  5. $ egrep -o 's[0-9]*SM' metar.txt | tr -d '[A-Z]'
  6. 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:

  1. visib=$(egrep -o '\s[0-9]*SM' metar.txt)
  2. # this strips the "SM" part off the end
  3. visib="${visib//SM}"
  4. # print it...
  5. printf "\nVisibility: %d Statute Miles\n" $visib

Output:

  1. # populate file with content
  2. $ echo "METAR KABC 121755Z AUTO 21016G24KT 180V240 1SM R11/P6000FT -RA BR BKN015 0VC025 06/04 A2990" > metar.txt
  3. # extract the visibility part
  4. $ visib=$(egrep -o '\s[0-9]*SM' metar.txt)
  5. # strip off the units
  6. visib=${visib//SM}
  7. # print results
  8. $ printf "\nVisibility: %d Statute Miles\n" "$visib"
  9. Visibility: 1 Statute Miles

You can also do something like this to delete the units:

  1. $ egrep -o '\s[0-9]*SM' metar.txt
  2. 13SM
  3. $ egrep -o '\s[0-9]*SM' metar.txt | tr -d 'SM'
  4. 13
  5. $ egrep -o '\s[0-9]*SM' metar.txt | tr -d '[A-Z]'
  6. 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:

确定