5

I am fairly new to grep and sed commands.How can +50.0 be extracted from Core 0: +50.0°C (high = +80.0°C, crit = +90.0°C) using grep or sed in bash script?

acpitz-virtual-0
Adapter: Virtual device
temp1:        +50.0°C  (crit = +89.0°C)
temp2:        +50.0°C  (crit = +89.0°C)

coretemp-isa-0000
Adapter: ISA adapter
Core 0:       +50.0°C  (high = +80.0°C, crit = +90.0°C)
Core 2:       +47.0°C  (high = +80.0°C, crit = +90.0°C)

Bash Script:

#!/bin/bash

temp=`sed -n '/^Core 0:      $/,/^(high/p' ~/Desktop/sensors.txt`

echo $temp
curious_coder
  • 2,392
  • 4
  • 25
  • 44
  • Do you have more specific rules about what needs to be extracted? Is it always just immediately after `Core 0:` but before `(`? – Explosion Pills Sep 05 '13 at 17:49
  • I just want to extract whatever present in-between Core 0: and (. If possible eliminate the extra space between Core 0: and +50.0(which is dynamic) – curious_coder Sep 05 '13 at 17:52

4 Answers4

2

Using grep -oP (PCRE regex):

grep -oP 'Core 0: +\K[+-]\d+\.\d+' ~/Desktop/sensors.txt
+50.0
anubhava
  • 761,203
  • 64
  • 569
  • 643
2
grep -e '^Core 0:' [input] | awk '{ print $3 }'

Or alternately, just in awk,

awk '{ r = "^Core 0:" } { if(match($0,r)) { print $3 } }' [input]

Both of these will print the field you seem to be looking for, with the surrounding whitespace eliminated.

qaphla
  • 4,707
  • 3
  • 20
  • 31
  • `grep -e '^Core 0:' [input] | awk '{ print $3 }'` is printing `device (crit (crit adapter +50.0°C +47.0°C` . I just need +50.0 – curious_coder Sep 05 '13 at 18:02
  • It looks like the grep isn't working for some reason, and the awk code is just running over all lines. No idea why that would be happening, though -- both run fine on my machine. – qaphla Sep 05 '13 at 18:09
  • [Don't use `grep | awk` anyway.](http://partmaps.org/era/unix/award.html#grep) Simply `awk '/^Core 0:/ { print $3 }' input` does that much more elegantly, succinctly, and idiomatically. – tripleee Sep 05 '13 at 19:19
2

Use grep passing in the perl-regex flag and use \K to discard the un-needed portions of the line to the left

 grep -oP '^Core 0:\s+\K\+\d+[.]\d+?' 
iruvar
  • 22,736
  • 7
  • 53
  • 82
1

For completeness sake, here is an attempt at fixing your sed script.

sed is line oriented, so your attempt would print from anything matching the first expression (which would never match anyway) to the following line matching the second.

(So, to rephrase, sed -n '/first/,/last/p' file will print from the first line in file matching first, and stop when it finds last on a subsequent input line from the file.)

Try this instead:

sed -n 's/^Core 0: *//;T;s/ (high.*//p' ~/Desktop/sensors.txt

This will replace the first expression with nothing. If the replacement did not occur, T jumps to the end of the script (implicitly doing nothing, just starting over with the next input line). Otherwise, we have a match, so we also replace the tail with nothing, and print.

... However, not all sed dialects have the T command, and some apparently dislike scripts separated by semicolons.

tripleee
  • 175,061
  • 34
  • 275
  • 318