2016-01-27 14 views
0

TL:DR我希望緊跟在'+'符號之後的任何行的前兩個數字的語法。在 +號後加上第一個(2位數)數字

給出下面的文本(從熟悉的實用程序):

power_meter-acpi-0 
Adapter: ACPI interface 
power1:  4.29 MW (interval = 4294967.29 s) 

coretemp-isa-0000 
Adapter: ISA adapter 
Physical id 0: +44.0°C (high = +75.0°C, crit = +85.0°C) 
Core 0:   +36.0°C (high = +75.0°C, crit = +85.0°C) 
Core 1:   +38.0°C (high = +75.0°C, crit = +85.0°C) 
Core 2:   +36.0°C (high = +75.0°C, crit = +85.0°C) 
Core 3:   +36.0°C (high = +75.0°C, crit = +85.0°C) 
Core 4:   +37.0°C (high = +75.0°C, crit = +85.0°C) 
Core 5:   +36.0°C (high = +75.0°C, crit = +85.0°C) 

coretemp-isa-0001 
Adapter: ISA adapter 
Physical id 1: +43.0°C (high = +75.0°C, crit = +85.0°C) 
Core 0:   +36.0°C (high = +75.0°C, crit = +85.0°C) 
Core 1:   +38.0°C (high = +75.0°C, crit = +85.0°C) 
Core 2:   +36.0°C (high = +75.0°C, crit = +85.0°C) 
Core 3:   +37.0°C (high = +75.0°C, crit = +85.0°C) 
Core 4:   +36.0°C (high = +75.0°C, crit = +85.0°C) 
Core 5:   +37.0°C (high = +75.0°C, crit = +85.0°C) 

我需要得到有趣的數字出來,即44,36,38,36,36,37,等等

從Linux的命令行,我用sensors | awk '{ print $3 }' | egrep -o '\+..' | sed 's/^.//'輸出14個有趣的數字中的12個,並沒有那麼優雅。

+0

'sensors | sed -r's /^.*\+([[:digit:]] +。[[:digit:]]。C。*/\ 1 /' – bjhaid

+0

@EdMorton這是「太長了,閱讀「 - 提供摘要。 –

回答

1

也許,在一個awk的一行:

sensors | awk -F'[+.]' '/\+/{print $2}' 

,它假定數字與 '+' 開始。如果溫度可能與一個減號開始,你可能要添加:

sensors | awk -F'[-+.]' '/high/{print $2}' 

真的,要看是什麼你肯定,你可以關閉的鍵。

而且,當然,如果溫度真的是以負數開始,那麼您可能會遇到更大的問題。 :-)

1

你可以用sed和grep結合:

$ sensors | sed 's/^[^+]*+\([0-9][0-9]\).*$/\1/g' | grep -o "^[0-9][0-9]$" 
44 
36 
38 
36 
36 
37 
36 
43 
36 
38 
36 
37 
36 
37 

sed命令每一行映射到它的第一雙位數。 grep命令過濾掉每個不是兩位數的行。

+0

如果您從'sed'中刪除'/ g',則不需要'grep',因爲您不再需要全局替換 – bjhaid

+0

仍然需要'grep'來過濾空行和標題行。 –

1
$ sed -n 's/[^+]*+\([0-9][0-9]\).*/\1/p' file 
44 
36 
38 
36 
36 
37 
36 
43 
36 
38 
36 
37 
36 
37 
+0

這個也出現在VLQ中。 –

1

單一的grep可以做到這一點,如果我們採取的事實,那就是左括號後面的第一個數字的地方,而不是在別人後面優勢:

$ grep -oP '(?<=\+|-)\d+(?=\.\d°C\s+\()' infile 
44 
36 
38 
36 
36 
37 
36 
43 
36 
38 
36 
37 
36 
37 

用Perl的正則表達式引擎(-P)並且僅提取匹配(-o):

(?<=\+|=)  # Positive look-behind: + or - 
\d+    # The digits we're extracting 
(?=\.\d°C\s+\() # Positive look-ahead: dot digit °C spaces and (
+0

我真的很感激有人評論他們的正則表達式 - 這是一個鈍的腳本。 – user121330

相關問題