1

I have a text file with the following contents:

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  

I need to parse the numbers from the bolded lines using a bash script but I haven't been able to do this with the command set I have available. This is my first time every using BASH scripts and the searches I've found that could help used some grep, sed, and cut options that weren't available. The options I have are listed below:

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'  

Can someone please help me with this? Thanks!

4 Answers 4

4

AWK can do that for you:

awk '/^(FEC.*Block|Good Block|BER|Post)/{print $NF}' textfile
Sign up to request clarification or add additional context in comments.

9 Comments

When I tried this command: "awk '/^(FEC.*Block|Good Block|BER|Post)/{print $NF}' tuner_0", I got the output: "tune".
@Ricardo: Is tuner_0 a file that contains the lines you posted in your question? If so, it should give you the numbers at the ends of the lines you bolded. I tested this by copying those lines into a file and running it with BusyBox 1.13 AWK.
@Dennis tuner_0 contains the text, yes. I don't understand why it's not working either.
@dennis-williamson I have more information about the awk command you suggested. tuner_0 is constantly being updated with values. I copied tuner_0 to another folder and the command worked. Any ideas on why the awk-only solution fails but the grep one below works in this case?
@Ricardo: AWK probably sees the file as it exists at the moment it's read. You may need to line buffer the file before AWK gets it. Try tail -f tuner_0 | awk ....
|
0
grep -e "^FEC " -e "^Good Block" -e "BER" file.txt | awk '{print $NF}'

grep: Match lines that: start with FEC or start with Good Block or contains BER

awk: Print the last space-separated field in each line

1 Comment

I was able to get the numbers I wanted with this answer.
0

If you have the right grep, you can do this with grep alone, using a regex look-ahead:

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

I got the inspiration for this here

3 Comments

Firstly this question is over 2 years old and the OP doesn't have GNU Grep so they don't have -P. They are on a BusyBox system and have posted the available options. You'd be better off spending your time answering new question.
@sudo_O - Thanks - I totally missed the dates on this question and answers - I'm not really sure how this popped up for me. Your point about grep -P is well taken - again I neglected that detail. Regardless, this question piqued my interest - it was definitely worth it for my own learning to spend a few minutes researching this. I hope I haven't wasted anyone else's time in doing so.
You haven't wasted anybodies time and it's good you learned something, you will get more recognition for your answers for new question.
0

In addition, you can do this with a pure bash one-liner, no awk, sed, grep, or other helpers:

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

or

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.