简体   繁体   English

从可变输出获取特定行

[英]Get Specific Line From Variable Output

I need to get a line from a commands output that is assigned to a bash variable. 我需要从分配给bash变量的命令输出中获得一行。

#Gets the netstat values for the UPD packages
UDP_RAW_VALUE=`netstat -su`

##TRYING THIS BUT TOTALLY FAILED.
echo "$UDP_RAW_VALUE" | sed -n "$2"p 

I just need to get a specific value from one of the command executions, which is 我只需要从命令执行之一中获取特定值,即

UDP_RAW_VALUE=`netstat -su`

The output of this specific command is like this: 该特定命令的输出如下所示:

-bash-4.2$ bash MyBashScript
IcmpMsg:
    InType0: 14464
    InType3: 12682
    InType8: 101
    InType11: 24
    OutType0: 101
    OutType3: 34385
    OutType8: 15840
Udp:
    931752 packets received
    889 packets to unknown port received.
    0 packet receive errors
    1007042 packets sent
    0 receive buffer errors
    0 send buffer errors
    IgnoredMulti: 647095
UdpLite:
IpExt:
    InMcastPkts: 1028470
    InBcastPkts: 552859
    InOctets: 233485843587
    OutOctets: 75548840236
    InMcastOctets: 44792084
    InBcastOctets: 167490770
    InNoECTPkts: 317265390
    InECT0Pkts: 25289
    InCEPkts: 686361

Simply, I need to read the numeric value of the 931752 packets received result, excluding the "packets received" part. 简单来说,我需要读取931752数据包接收结果的数值不包括“已接收数据包”部分。 I have tried doing some regular expression. 我尝试做一些正则表达式。 Then found this sed which looks promising but I have no idea how to use it, in terms of regular expression. 然后发现这个sed看起来很有希望,但是我不知道如何使用它,就正则表达式而言。 I am pretty sure I need to complete this with regex. 我很确定我需要用正则表达式完成此操作。

I need the numerical value because I will compare a previous and current value and check if the difference is between the threshold. 我需要数字值,因为我将比较先前的值和当前值,并检查差异是否在阈值之间。 If not, send the mail. 如果没有,请发送邮件。

Just add this: 只需添加:

 PACKETS_RECEIVED=$(echo "${UDP_RAW_VALUE}" | sed -n 's/^ *\([0-9][0-9]*\) packets received$/\1/p')

It will print the content that you saved from the RAW output of the command. 它将打印从命令的RAW输出保存的内容。 the "" are important or it will print all the output in one line which is not what you want. ""很重要,否则它将在不需要的一行中打印所有输出。

The sed command -n says do not print the lines by default. sed命令-n表示默认情况下不打印行。 the s///p command tells is to substitute the pattern between the first two / / with the \\1 , which is the group in the pattern that is between \\( and \\) ; s///p命令告诉是替代模式的前两个之间/ /\\1 ,这是在图案中的基团,其之间\\(\\) ; in our case that would be the number you want. 在我们的情况下,这就是您想要的电话号码。

The PACKET_RECEIVED=$() part tells bash to run what is between the $() and assign the output to the variable. PACKET_RECEIVED=$()部分告诉bash运行$()之间的内容,并将输出分配给变量。

perhaps another sed solution if you dont mind? 如果您不介意,也许是另一种sed解决方案?

UDP_RAW_VALUE=`netstat -su|sed -n '/Udp/{n;s/[^0-9\n]//g;p}'`

/Udp:/ #find all lines that have Udp /Udp:/ #查找所有具有Udp的行

n # get the next line delete all all characters leaving you number n #获取下一行,删除所有剩下的数字

p #print p #打印

or awk 或awk

   UDP_RAW_VALUE=`netstat -su|awk '$0~/Udp:/{getline;print $1}'`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM