grep/awk usage

Hi everybody, i know this question is common on the forum and i've searched for my answer but haven't quite found it.
I'm trying to extract some values from a number of log files which look like this:

Peak Power Consumption: 0.20777 Watts

Observed Average Power: 0.1414794247 Watts

The other log files are similar where the value i want to extract is behind a word and a colon (like here 'Peak Power Consumption:')

I want to extract the values behind the colons using grep (or something else, whatever is easiest) and i want to use the text (or a piece of the text) before the colon and the colon as pattern so i can do the same trick to the other log files i have to process.

Anyone has any ideas?
Thanks a lot in advance!

Hi,

Can you post the expected output ?

Sure, i want to get variables in the script with the values, so for example after the grep i want to have:
$result1 = 0.20777
$result2 = 0.1414794247

check if this would suffice you

 awk -F: '{print $1}' < filename

This outputs:

Peak Power Consumption Oberserved Average Power

so not the desired output, but thanks for helping :slight_smile:

awk -F":" '{print $2}' testfile | awk '$1 ~ /[0-9]/ {print $1}'
1 Like

Assuming you have the same unit always i.e watts and the colon appears only once in a line.

$ eval $(awk '/[a-zA-Z]+:/ { gsub(".*: ", "");gsub(" Watts", "");print "result"NR"="$0}' logfile)
$ 
$ 
$ echo $result2
0.20777
$ echo $result4
0.1414794247
$ 

If the scenario is different, please mention all the possible cases.

1 Like

@pravin

Your code works ok, when i use:

BatteryResult=`awk -F":" '{print $2}' Battery.log | awk '$1 ~ /[0-9]/ {print $1}'`
echo $BatteryResult

I get output:

0.20777 0.1414794247

But i want these values in separate variables.

@anchal_khare

It's only Watts in this log file, in the other log files there is nothing behind the value i want to extract

Hi

Are you looking for something like this:

awk -F: 'NF{split($2,a," ");print "Result"i++" "a[1]}' i=1  file

Output:

Result1 0.20777
Result2 0.1414794247

Guru.

1 Like

@guruprasadpr

Yes that's it :slight_smile: Thanks

I found a way of processing pravin's code also to make it work:

BatteryResult=`awk -F":" '{print $2}' Battery.log | awk '$1 ~ /[0-9]/ {print $1}'`
echo $BatteryResult
res1=`echo $BatteryResult | awk '{print $1}'`
res2=`echo $BatteryResult | awk '{print $2}'`
echo $res1
echo $res2

Here $res1 and $res2 contain the correct values also!

Thanks a lot for helping guys, i think my problem is solved.