teakmeister
teakmeister

Reputation: 25

Extracting value from ipmitool text file using bash

I'm using Linux (Raspberry Pi) and bash. I'm using the Pi to pull IPMI sensor info from a Dell iDRAC. I've managed to pulled the sensor information off to a txt file, sample below:

Fan1 RPM         | 2400.000   | RPM        | ok    | na        | 600.000   | 840.000   | na        | na        | na

Fan2 RPM         | 2400.000   | RPM        | ok    | na        | 600.000   | 840.000   | na        | na        | na

Fan3 RPM         | 2400.000   | RPM        | ok    | na        | 600.000   | 840.000   | na        | na        | na

Fan4 RPM         | 2280.000   | RPM        | ok    | na        | 600.000   | 840.000   | na        | na        | na

Fan5 RPM         | 2400.000   | RPM        | ok    | na        | 600.000   | 840.000   | na        | na        | na

Fan6 RPM         | 2520.000   | RPM        | ok    | na        | 600.000   | 840.000   | na        | na        | na

Inlet Temp       | 11.000     | degrees C  | ok    | na        | -7.000    | 3.000     | 42.000    | 47.000    | na

Exhaust Temp     | 18.000     | degrees C  | ok    | na        | 3.000     | 8.000     | 70.000    | 75.000    | na

Temp             | na         |            | na    | na        | na        | na        | 0.000     | na        | na

Temp             | na         |            | na    | na        | na        | na        | 0.000     | na        | na

What I would like to achieve to to extract the values in the second column, such as Fan1 RPM value of 2400.000 and dump this to a file such as fan1.txt

I'm guessing this is possible using something like sed, but I'm a bit of a newbie in this area.

Upvotes: 1

Views: 590

Answers (4)

Puwen
Puwen

Reputation: 61

ipmitool -I lanplus -H BMC_IP -U BMC_USER -P BMC_PASS sensor reading 'Fan1 RPM'|awk '{print $NF}'

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133508

Could you please try following.

awk 'match($0,/Fan1 RPM +\| +[0-9]+\.[0-9]+/){val=substr($0,RSTART,RLENGTH);sub(/.* /,"",val);print val}'  Input_file

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203502

Any time you're talking about input with "columns" (fields), the tool you want to use is awk, not grep or sed since awk is specifically designed to work on fields while those other tools aren't.

$ awk -F' *[|] *' '$1=="Fan1 RPM"{print $2}' file
2400.000

$ awk -F' *[|] *' '($1=="Inlet Temp") && ($7 >= 3) {print $6, $9, $10}' file
-7.000 47.000 na

or just to convert to a CSV so it can be read by Excel, etc.:

$ awk -F' *[|] *' -v OFS=',' 'NF{$1=$1;print}' file
Fan1 RPM,2400.000,RPM,ok,na,600.000,840.000,na,na,na
Fan2 RPM,2400.000,RPM,ok,na,600.000,840.000,na,na,na
Fan3 RPM,2400.000,RPM,ok,na,600.000,840.000,na,na,na
Fan4 RPM,2280.000,RPM,ok,na,600.000,840.000,na,na,na
Fan5 RPM,2400.000,RPM,ok,na,600.000,840.000,na,na,na
Fan6 RPM,2520.000,RPM,ok,na,600.000,840.000,na,na,na
Inlet Temp,11.000,degrees C,ok,na,-7.000,3.000,42.000,47.000,na
Exhaust Temp,18.000,degrees C,ok,na,3.000,8.000,70.000,75.000,na
Temp,na,,na,na,na,na,0.000,na,na
Temp,na,,na,na,na,na,0.000,na,na

Upvotes: 1

KamilCuk
KamilCuk

Reputation: 141000

Grep for the value and cut it.

grep "Fan1 RPM" input_file | cut -d'|' -f2 > fan1.txt

You could also pipe it via tr -d ' ' or tr -d '[:space:] to remove (white-)spaces.

Upvotes: 1

Related Questions