【问题标题】:Parsing out numbers from a string - BASH script从字符串中解析出数字 - BASH 脚本
【发布时间】:2011-03-16 15:15:40
【问题描述】:

我有一个包含以下内容的文本文件:

QAM Mode : QAM-16  
QAM Annex : Annex A  
Frequency : 0 Hz  
IF Frequency : 0 Hz  
Fast Acquisition  : 0  
Receiver Mode  : cable  
QAM Lock : 1  
FEC Lock : 1  
Output PLL Lock : 0  
Spectrum Inverted : 0  
Symbol Rate : -1  
Symbol Rate Error : 0  
IF AGC Level (in units of 1/10 percent) : 260  
Tuner AGC Level (in units of 1/10 percent) : 1000  
Internal AGC Level (in units of 1/10 percent) : 0  
SNR Estimate (in 1/100 dB) : 2260  
**FEC Corrected Block Count (Since last tune or reset) : 36472114  
FEC Uncorrected Block Count (Since last tune or reset) : 0  
FEC Clean Block Count (Since last tune or reset) : 0**  
Cumulative Reacquisition Count : 0  
Uncorrected Error Bits Output From Viterbi (Since last tune or reset) : 0  
Total Number Of Bits Output from Viterbi (Since last tune or reset) : 0  
viterbi bit error rate (in 1/2147483648 th units) : 0  
Carrier Frequency Offset (in 1/1000 Hz) : -2668000  
Carrier Phase Offset (in 1/1000 Hz) : 0  
**Good Block Count (Reset on read) : -91366870**  
**BER Raw Count (Reset on read) : 0**  
DS Channel Power (in 10's of dBmV units ) : -760  
Channel Main Tap Coefficient : 11846  
Channel Equalizer Gain Value in dBm : 9  
**Post Rs BER : 2147483648  
Post Rs BER Elapsed Time (in Seconds) : 0**  
Interleave Depth : 1  

我需要使用 bash 脚本解析粗体行中的数字,但我无法使用可用的命令集来执行此操作。这是我第一次使用 BASH 脚本和我发现的搜索可以帮助使用一些不可用的 grep、sed 和 cut 选项。下面列出了我的选项:

grep

Usage: grep [-ihHnqvs] PATTERN [FILEs...]  
Search for PATTERN in each FILE or standard input.  
Options:  
        -H      prefix output lines with filename where match was found  
        -h      suppress the prefixing filename on output  
        -i      ignore case distinctions  
        -l      list names of files that match  
        -n      print line number with output lines  
        -q      be quiet. Returns 0 if result was found, 1 otherwise  
        -v      select non-matching lines  
        -s      suppress file open/read error messages  

sed

BusyBox v1.00-rc3 (00:00) multi-call binary  
Usage: sed [-efinr] pattern [files...]  
Options:  
        -e script       add the script to the commands to be executed  
        -f scriptfile   add script-file contents to the  
                        commands to be executed  
        -i              edit files in-place  
        -n              suppress automatic printing of pattern space  
        -r              use extended regular expression syntax  
If no -e or -f is given, the first non-option argument is taken as the sed  
script to interpret. All remaining arguments are names of input files; if no  
input files are specified, then the standard input is read.  Source files  
will not be modified unless -i option is given.  

awk

BusyBox v1.00-rc3 (00:00) multi-call binary  
Usage: awk [OPTION]... [program-text] [FILE ...]  
Options:  
        -v var=val              assign value 'val' to variable 'var'  
        -F sep                  use 'sep' as field separator  
        -f progname             read program source from file 'progname'  

有人可以帮我解决这个问题吗?谢谢!

【问题讨论】:

    标签: bash sed awk grep


    【解决方案1】:

    AWK 可以为您做到这一点:

    awk '/^(FEC.*Block|Good Block|BER|Post)/{print $NF}' textfile
    

    【讨论】:

    • 当我尝试这个命令时:“awk '/^(FEC.*Block|Good Block|BER|Post)/{print $NF}' tuner_0”,我得到了输出:“tune” .
    • @Ricardo:tuner_0 是包含您在问题中发布的行的文件吗?如果是这样,它应该在您加粗的行的末尾为您提供数字。我通过将这些行复制到一个文件中并使用 BusyBox 1.13 AWK 运行它来测试它。
    • @Dennis tuner_0 包含文本,是的。我也不明白为什么它也不起作用。
    • @dennis-williamson 我有更多关于你建议的 awk 命令的信息。 tuner_0 会不断更新值。我将 tuner_0 复制到另一个文件夹并且该命令有效。关于为什么仅 awk 的解决方案失败但下面的 grep 解决方案在这种情况下有效的任何想法?
    • @Ricardo:AWK 可能会看到文件在读取时就存在。您可能需要在 AWK 获取文件之前对文件进行行缓冲。试试tail -f tuner_0 | awk ...
    【解决方案2】:
    grep -e "^FEC " -e "^Good Block" -e "BER" file.txt | awk '{print $NF}'
    

    grep:匹配行:以FEC开头Good Block开头包含BER

    awk: 打印每行最后一个以空格分隔的字段

    【讨论】:

    • 我能够通过这个答案得到我想要的数字。
    【解决方案3】:

    如果您有正确的 grep,您可以单独使用 grep,使用正则表达式前瞻:

    $ /bin/grep -Po "(?<=Post Rs BER : )(.+)" data.txt 
    2147483648
    $
    

    我得到了这个here的灵感

    【讨论】:

    • 首先这个问题已经超过 2 年了,OP 没有 GNU Grep 所以他们没有 -P。他们在 BusyBox 系统上并发布了可用选项。你最好把时间花在回答新问题上。
    • @sudo_O - 谢谢 - 我完全错过了这个问题和答案的日期 - 我不确定这是如何为我弹出的。你关于 grep -P 的观点很好 - 我再次忽略了那个细节。无论如何,这个问题激起了我的兴趣——我自己学习花几分钟研究这个绝对是值得的。我希望我没有浪费其他人的时间。
    • 你没有浪费任何人的时间,你学到了一些东西很好,你对新问题的回答会得到更多的认可。
    【解决方案4】:

    此外,您可以使用纯 bash 单行代码来执行此操作,无需 awk、sed、grep 或其他帮助程序:

    $  { while read line; do if [[ $line =~ "Post Rs BER : (.*)$" ]]; then echo ${BASH_REMATCH[1]}; fi; done; } < data.txt 
    2147483648
    $
    

    $ cat data.txt | { while read line; do if [[ $line =~ "Post Rs BER : (.*)$" ]]; then echo ${BASH_REMATCH[1]}; fi; done; }
    2147483648
    $ 
    

    【讨论】:

      猜你喜欢
      • 2015-09-04
      • 1970-01-01
      • 1970-01-01
      • 2023-01-31
      • 1970-01-01
      • 2021-06-25
      • 2017-07-16
      • 2011-03-03
      相关资源
      最近更新 更多