从字符串 - BASH脚本中解析数字

时间:2011-03-16 15:15:41

标签: bash sed awk grep

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

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'  

有人可以帮我这个吗?谢谢!

4 个答案:

答案 0 :(得分:4)

AWK可以为您做到这一点:

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

答案 1 :(得分:0)

grep -e "^FEC " -e "^Good Block" -e "BER" file.txt | awk '{print $NF}'

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

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

答案 2 :(得分:0)

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

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

我得到了这个here

的灵感

答案 3 :(得分:0)

此外,您可以使用纯粹的bash one-liner,没有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
$