grep: how to get number between two pieces of text - regex

I want to grep my cpu temp.
When i type in:
ipmi-sensors -s 2352
I get:
2352: CPU Temp (Temperature): 30.00 C (NA/81): [OK]
I would like to grep the number between "(Temperature):" and "C"

Using grep -oP:
ipmi-sensors -s 2352 | grep -oP '\(Temperature\):\s*\K[\d.]+'
30.00
If your grep doesn't support -P option then use this awk:
ipmi-sensors -s 2352 | awk -F '.*\\(Temperature\\): *| C .*' '{print $2}'
30.00

Try this way also
ipmi-sensors -s 2352 | awk -F'[: ]' '{print $7}'
sed version
ipmi-sensors -s 2352 | sed 's/[^)]\+): \([^ ]\+\).*/\1/'
Output:
30.00

Related

Terminal/Bash - Return until tab

I have this string;
4.0K /Server/mysql/backup/backup_mysql_ltr_20151006-131057.tar.gz
and I need only the 4.0K returned, however obviously, this number could be anything. I believe there is a tab between the K and the /Server
Any ideas how it could be done. It'll be used in a terminal/bash command on Mac OS X and Ubuntu.
<command that produces your output> | cut -f 1
And if that is not a tab between the K and the /Server, then
<command that produces your output> | cut -f 1 -d' '
One approach is to just use awk:
sz=$(echo "$string" | awk '{print $1}')
as per the following transcript:
pax> string="4.0K /Server/blah_blah_blah.tar.gz"
pax> sz=$(echo "$string" | awk '{print $1}')
pax> echo $sz
4.0K
There are many other approaches using cut, sed, grep -o and so on(1) but I usually use awk because:
it lends itself naturally to white-space separated fields; and
it tends to allow for more powerful programming than the others.
(1) Such as:
sz=$(echo "$string" | cut -f1)
sz=$(echo "$string" | sed 's/\t.*$//')
sz=$(echo "$string" | grep -o $'^[^\t]*')
and so on...
s="4.0K /Server/mysql/backup/backup_mysql_ltr_20151006-131057.tar.gz"
x="${s// *}"
echo "$x"
Output:
4.0K

Combination of same delimiters in awk

I have a String
1__2_3__4_5_6
I want to set '__'(2 underscore) as delimiter in AWK.
$1 should be 1
$2 should be 2_3
$3 should be 4_5_6
Just set __ as FS value. You could also pass a regex as FS value.
$ echo '1__2_3__4_5_6' | awk -v FS="__" '{print $1}'
1
$ echo '1__2_3__4_5_6' | awk -v FS="__" '{print $2}'
2_3
$ echo '1__2_3__4_5_6' | awk -v FS="__" '{print $3}'
4_5_6
$ echo '1__2_3__4_5_6' | awk -v FS="_{2}" '{print $3}'
4_5_6
_{2} matches exactly two underscores.

How to display part of matched pattern in grep?

I wanted to extract 12 from a text like "abc_12_1". I am trying like this
echo "abc_12_1" | grep -Eo '[a-zA-Z]+_[0-9]+_1'
abc_12_1
But I am not able to select the digit after first _ in string, the output of above command is whole string. I am looking for some alternative in grep which I have in following Perl pattern matching.
perl -e '"abc_55_1" =~ m/[a-zA-Z]+_([0-9]+)_1/ ; print $1'
55
Is it possible with grep?
Using perl:
$ echo "abc_12_1" | perl -lne 'print /_(\d+)_/'
12
or grep:
$ echo "abc_12_1" | grep -oP '(?<=_)\d+(?=_)'
12
You could use cut:
cut -d_ -f2 <<< "abc_12_1"
Using grep:
grep -oP '(?<=_).*?(?=_)' <<< "abc_12_1"
Both would yield 12.
One way is to use awk
echo "abc_12_1" | awk -F_ '{print $2}'
12
Or grep
echo "abc_12_1" | grep -o "[0-9][0-9]"
12
Using grep with extended regex
grep -oE "[0-9]{2}" # Get only hits with two digits
grep -oE "[0-9]{2,}" # Get hits with two or more digits

egrep string case

I have to grep from a file name temp which has something like this
Process State
BE_RP:1 [PL_2_3] Running
BE_RP:2 [PL_2_4] Running
BE_RP:3 [PL_2_5] Running
BE_RP:4 [PL_2_6] Running
FE_SCTP:0 [PL_2_3] Running
FE_SCTP:1 [PL_2_4] Running
BE_NMP:0 Not Running
OAM:0 Running
I need to write a egrep statement which will return the number of process which are in running or not running state.
awk '/^OAM/ { next } /Not Running[ \t]*$/{s++} END {print s, NR-s-1}' foo.txt
Prints <running> <not running>
Running
$ grep -v 'OAM' input | grep -cP '(?<!Not) Running\s*$'
6
Not Running
$ grep -v 'OAM' input | grep -cP 'Not Running\s*$'
1
sed '{
1 d
s/^[^:]*:[0-9]*[ ]*//
s/^[^]]*]//
s/^[ ]*//
}' input_file | sort | uniq -c
grep -P '^(?!OAM:0).*Running' temp | cut -f2 | wc -l

using sed to get only line number of "grep -in"

Which regexp should I use to only get line number from grep -in output?
The usual output is something like this:
241113:keyword
I need to get only "241113" from sed's output.
I suggest cut
grep -in keyword ... | cut -d: -f1
If you insist with sed:
grep -in keyword ... | sed 's/:.*$//g
You don't need to use sed. Cut is enough. Just pipe grep's output to
cut -d ':' -f 1
As an example:
grep -n blabla file.txt | cut -d ':' -f 1
Personally, I like awk
grep -in 'search' file | awk --field-separator : '{print $1}'
As said in other answers, cut is the right tool; but if you really want to use a swiss-army knife, you can also use awk:
grep -in keyword ... | awk -F: '{print $1}'
or using grep again:
grep -in keyword ... | grep -oE '^[0-9]+'
Just in case someone is wondering if all this could be done without grep, i.e. with sed alone ...
echo '
a
b
keyword
c
keyWord
x
y
keyword
Keyword
z
' |
sed -n '/[Kk][Ee][Yy][Ww][Oo][Rr][Dd]/{=;}'
#sed -n '/[Kk][Ee][Yy][Ww][Oo][Rr][Dd]/{=;q;}' # only line number of first match