Grep rest of line…after match

awkgrepsedtext processing

I have a file containing only two lines, with the following structure:

$ cat /tmp/pwpower.log
000D6F0000D34227, -114.10
000D6F0001A405C4, -130.09

The values are power values of my solar plant. Negative value means generation.

I would need the values extracted via grep/sed/awk – whatever is the smartest way. I need to have both values extracted separately and without the minus sign.

What I do now is kind of stupid but it works – I'm sure many of you will have smarter ways for me 🙂 Here of course I only see the values plus Minus.

To get the first value:

cat /tmp/pwpower.log |grep -o "\-.*" | head -n 1

To get the second value:

cat /tmp/pwpower.log |grep -o "\-.*" | tail -n1

And related question, is there a simple way to take these STRINGs and transform so that I can calculate the SUM ?

Best Answer

All values:

$ awk -F '[ -]*' '$0=$NF' /tmp/pwpower.log
114.10
130.09

Value on first line:

$ awk -F '[ -]*' 'NR==1{print $NF;exit}' /tmp/pwpower.log
114.10

Value on second line:

$ awk -F '[ -]*' 'NR==2{print $NF;exit}' /tmp/pwpower.log
130.09

Sum of all values:

$ awk -F '[ -]*' '{sum+=$NF} END{print sum}' /tmp/pwpower.log
244.19
Related Question