Saira
Saira

Reputation: 121

Extracting numbers from the variable in bash script

I have a bash script which outputs some statistics on the screen for each sample number(example shown below) when I run the test.sh code.

What I am trying to do is that to store the output in a variable and extract the Mean value for a testNum --> 11 and 52

A=`./test.sh`    # I have all the output in a variable A 

Now I need to extract the mean value of 11 which is -128 and -96 for 52 I'm trying and thinking How I can do this

Can anyone help me in this please ?

This is the example test code : test.sh

#!/bin/sh
echo Valid Numbers per States:3G phones
echo testNum        N_States         Mean Value 
echo    1       10              -128 
echo    2       10      -95 
echo    3       10      -94 
echo    4       10      -94 
echo    5       10      -94 
echo    6       10      -128    
echo    7       10      -91 
echo    8       10      -94 
echo    9       10      -94     
echo    10      10      -94 
echo    11      10      -128    
echo --------------------------------------------   
echo Valid Numbers per States :4G phones        
echo testNum        N_States         Mean Value         
echo    36      10      -95 
echo    40      10      -95 
echo    44      10      -95 
echo    48      10      -95
echo    52      10      -96
echo    56      10      -95
echo    60      10      -96
echo    64      10      -96
echo    100             10      -99
echo    104             10      -97
echo    108             10      -98
echo    112             9       -98
echo    116             9       -98
echo    120             9       -99
echo    124             9       -98 
echo    128             9       -98             
echo    132             9       -98             
echo    136             9       -99             
echo    140             9       -99             
echo    144             9       -99             
echo    149             9       -98             
echo    153             9       -99 
echo    157             9       -99
echo    161             9       -99
echo    165             9       -98
echo --------------------------------------------



I have used the commands echo "$x" | grep -w '^52' | cut -d' ' -f3

But Linux on my proprietary hardware doesn't allow ^ (not sure if its version or something) .. I can run this on any bash shell..works fine, but if I run the same command, it doesn't output anything.

So I started doing some awk on it Here temp is 128 NF_Est is output of the script echo "$NF_Est" | grep -w 128 | awk '{if ($1==128)print $0}' | tr -s " " | cut -d" " -f3

But this is not working if I am the "temp" values in multiple columns

Any suggestions where I am messing up (or) this can be done in a much simpler way?

Upvotes: 0

Views: 925

Answers (2)

William Pursell
William Pursell

Reputation: 212634

You really want awk for this:

$ ./test.sh | awk '$1==11{print $3}'
-128
$ ./test.sh | awk '$1==52{print $3}'
-96

If you want to extract the value from $A instead of running the script, just do: echo "$A" | awk ...

Upvotes: 1

choroba
choroba

Reputation: 242343

You can use grep and cut to extract the information:

echo "$x" | grep -w '^11' | cut -d' ' -f3
echo "$x" | grep -w '^52' | cut -d' ' -f3

grep filters its input, outputting only lines that match the given pattern. ^ matches at the beginning of line. -w means "match whole words", without it, it would also output the lines 112 and 116.

cut extracts columns from its intput. -d specifies the delimiter, a space in this case, and -f says which columns to extract.

Upvotes: 1

Related Questions