1

I have result file where I need to read the value of "rx cells " the report file is as given as below: Content of test.log file is as follows:

    co_result=test.log  

    ZyXEL IES-1000> statistics port 12 0 33
    [adsl channel 12-0/33]
    tx packets: 56
    rx packets: 60
    tx rate   : 0
    rx rate   : 0
    tx cells  : 468
    rx cells  : 1707
    errors    : 0
    ZyXEL IES-1000>

I am using the following command to read the rx cells value:

    cells=$(grep -e 'cells' $co_result | cut -d: -f2 | awk '{print $1}'| awk 'NR%2{printf $0"";next;}1')
    echo "Var_value: $cells"

but when I tried to run the above 2 commands in a file. I am getting following output , where the first few characters in echo is not displaying:

       1707value: 530  

I am expecting only 1707 in $cells variable but it prints wrongly. I am not understanding the issue . Could you please help to fix it.

2

Your command substitution is unnecessarily complex.

Instead:

cells=$( awk '/rx cells/ { print $NF }' <"$co_result" )
printf 'Var_value: %s\n' "$cells"

$NF will be the last whitespace-delimited field on the line, and this is printed if the line contains the string rx cells.

Also make sure that the input file is a Unix text file and not a DOS text file. Run dos2unix on the file to convert it to a Unix text file.


Looking at your command:

grep -e 'cells' $co_result | cut -d: -f2 | awk '{print $1}'| awk 'NR%2{printf $0"";next;}1'

First: grep -e 'cells' $co_result would fail if $co_result contained a space or tab or newline, or any other special character (since it's unquoted).

The command outputs

tx cells  : 468
rx cells  : 1707

Then: cut -d: -f2. This outputs

 468
 1707

Then: awk '{print $1}'. This outputs the same as above, but with the initial spaces removed

Then: awk 'NR%2{printf $0"";next;}1'. This is an obfuscated way of doing paste -d' ' - - and outputs

468 1707

The echo later will output

Var_value: 468 1707

The reason you don't get that is that your input file is a DOS text file and contains carriage returns at the end of each line. Therefore, the cursor jumps back to the start of the line after printing 468.

Use dos2unix on you input file.

Where you get 530 from, I don't know.

  • Thank you for the detailed response. I tried simple grep command instead of awk as of now – Nagesh HS Jun 13 '18 at 9:24
1

You could simply use grep:

 cells=$(grep -F "rx cells" "$co_result" | grep -oE "[0-9]+")

this would extract the number into your variable by:

grep -F "rx cells" $co_result: this is extracting the line that contains rx cells : 1707
grep -oE "[0-9]+": only output the match of the regexp that matchs only number: 1707

You could also use the more elequant sed group substitution to extract the value like this:

cells=$(sed  -r -n 's/rx cells.*\W([0-9]+)/\1/p' "$co_result")
  • -r: enable extended regex
  • -n: only print explicit line
  • 's///p': s for substitute, p to explicitly print the line that match the pattern.
  • rx cells.*\W([0-9]+) match rx cells .*\W matches any character until a Non-word character and ([0-9]+) capture in group one or more digits.
  • \1 replace output by group one which contains n captured digits.
  • Thank you looks simple I tried the same thing with 2 stages. – Nagesh HS Jun 13 '18 at 9:25
  • but after getting the number 1707 , I am trying to compare it with other value max_pcr=100, its says max_pcr is not not number .. if [ $cells < $max_pcr ]; then echo " Co_result pass: $cells" else echo "Co_result fail" where the number max_pcr is not considered .. – Nagesh HS Jun 13 '18 at 9:28
  • @NageshHS Well I think the official syntax is if [ "$cells" -gt "$max_pcr" ] then but that's out of the scope of your initial question – Kiwy Jun 13 '18 at 10:15
  • Thank u, I have fixed the other comparison issue , anyway thanks for your inputs on actual issue. – Nagesh HS Jun 14 '18 at 3:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.