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
The sed command is a stream editor that works on streams of characters. It's a more powerful tool than grep as it offers more options for text processing purposes, including the substitute command, which sed is most commonly known for.
This collection of sed and grep use cases might help you better understand how these commands can be used in Linux. Tools like sed (stream editor) and grep (global regular expression print) are powerful ways to save time and make your work faster.
How do I extract text between two words ( <PRE> and </PRE> ) in unix or linux using grep command? Let us see how use the grep command or egrep command to extract data between two words or strings. I also suggest exploring sed/awk/perl commands to extract text between two words on your Linux or Unix machine.
Using grep -oP
(PCRE regex):
grep -oP 'Core 0: +\K[+-]\d+\.\d+' ~/Desktop/sensors.txt
+50.0
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With