0

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?

4
  • 1
    backticks were replaced decades ago with a cleaner notation. A=$(./test.sh) Commented Jul 11, 2019 at 19:26
  • 1
    Why are you unable to use ^? Does the computer crash or catch fire, or do you get a syntax error, or do you not have a keyboard with this symbol? Even if the system is proprietary, it almost certainly has a ^ character. Commented Jul 11, 2019 at 19:29
  • @tripleee U should have clearly read that..."" I can run this on any bash shell..works fine, but if I run the same command, it doesn't output anything "" Commented Jul 11, 2019 at 19:49
  • Thanks @WilliamPursell .. I will keep that in mind. I started shell scripting like in 3 days ago .. good to know that the backticks were replaced.. I will use (./test.sh) from now on. Commented Jul 11, 2019 at 19:53

2 Answers 2

1

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 ...

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @William ..this works great. I have these numbers 11 and 52 as a variable.. Do you by chance know what I am doing wrong here .. I am storing 11 and 52 in a $temp variable ``` echo "$A" | awk -v myvar="$temp" 'BEGIN {if ($1 eq myvar) print $3}' ``` why doing this wouldn't give me any output (as technically I am doing the same )
or even this .. ``` echo "$A" | awk -v myvar="$temp" '$1==myvar {print $3}' ```
@Saira You don't want to put the check in a BEGIN clause. You want it to run on each line of input. eg, echo "$A" | awk '$1 == k { print $3 }' k="$temp"
Thanks, @William . I understood where I am going wrong. Thanks for the guidance
1

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.

1 Comment

Thanks for the detailed answer ... Really appreciate it. But I have some issued with it as I am running this on a Linux system on proprietary hardware. For some reason, I cannot use '^' operator in the grep. I tried the following ``` echo "$Out_Var" | grep -w '128' | awk -v cx=$temp '{if ($1==cx) print }' ``` $temp is 128 , the number to be searched.... I have added my answer edit part of question.. Is there any way I can get away with awk ?

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.