How to grep and output block of string separated by spaces from a single line output

时间:2015-10-30 23:07:36

标签: linux bash grep

Is there a way to grep a single line of output with items separated by spaces and only output the exact block of string that begins and ends with spaces?

For instances, here is the output:

OK - Server: Supermicro Super Server s/n: 0123456789 System BIOS: 1.1 2015-04-09|P2Vol_0_Memory_Device_1_VDIMMAB=1.19;1.34;1.42 P2Vol_10_System_Board_19_3.3VSB=3.21;3.55;3.65 P2Vol_11_System_Board_20_1.5V_PCH=1.5;1.64;1.67 P2Vol_12_System_Board_21_1.2V_BMC=1.21;1.34;1.37 P2Vol_13_System_Board_32_3.3VCC=3.35;3.55;3.65 P2Vol_14_System_Board_33_5VCC=5;5.38;5.54 P2Vol_1_Memory_Device_2_VDIMMCD=1.19;1.34;1.42 P2Vol_2_Memory_Device_3_VDIMMEF=1.2;1.34;1.42 P2Vol_3_Memory_Device_4_VDIMMGH=1.19;1.34;1.42 P2Vol_4_Processor_3_Vcpu1=1.82;1.89;2.08 P2Vol_5_Processor_4_Vcpu2=1.82;1.89;2.08 P2Vol_6_System_Board_12_1.05V_PCH=1.05;1.19;1.22 P2Vol_7_System_Board_15_5VSB=5.07;5.38;5.54 P2Vol_8_System_Board_17_12V=12.18;12.94;13.25 P2Vol_9_System_Board_18_VBAT=2.87;3.67;3.78 P4Tem_0_Memory_Device_64_P1-DIMMA1_Temp=45;80;85 P4Tem_10_Processor_1_CPU1_Temp=47;80;85 P4Tem_11_Processor_2_CPU2_Temp=55;80;85 P4Tem_12_System_Board_1_System_Temp=32;80;85 P4Tem_13_System_Board_2_Peripheral_Temp=51;80;85 P4Tem_14_System_Board_3_PCH_Temp=44;90;95 P4Tem_1_Memory_Device_68_P1-DIMMB1_Temp=44;80;85 P4Tem_2_Memory_Device_80_P2-DIMME1_Temp=47;80;85 P4Tem_3_Memory_Device_84_P2-DIMMF1_Temp=45;80;85 P4Tem_4_Memory_Module_1_Vcpu1VRM_Temp=47;95;100 P4Tem_5_Memory_Module_2_Vcpu2VRM_Temp=54;95;100 P4Tem_6_Memory_Module_3_VmemABVRM_Temp=44;95;100 P4Tem_7_Memory_Module_4_VmemCDVRM_Temp=38;95;100 P4Tem_8_Memory_Module_5_VmemEFVRM_Temp=51;95;100 P4Tem_9_Memory_Module_6_VmemGHVRM_Temp=49;95;100 P5Fan_0_Fan_Device_1_FAN1=2000;25300;25400 P5Fan_1_Fan_Device_3_FAN3=2000;25300;25400 P5Fan_2_Fan_Device_5_FAN5=3100;25300;25400 P5Fan_3_Fan_Device_7_FANA=1900;25300;25400 P5Fan_4_Fan_Device_8_FANB=1800;25300;25400

I only want CPUx_Temp blocks from the whole output.

2 个答案:

答案 0 :(得分:0)

I'm not sure grep along will do it. Say 'foo' is a file containing your input line as shown. The code is: awk 'BEGIN{FS=";"};{for (i=1;i<=NF;i++) {if ($i~/Processor_[0-9]_CPU/) print $i} }' foo | cut -d_ -f5-9 Output: CPU1_Temp=47 CPU2_Temp=55 You requested capitalized instances of 'CPU' in proximity to their temps, so I think this is what you were looking for.

答案 1 :(得分:0)

You can use grep also: grep -Po 'CPU[0-9]_Temp=[0-9].?'