仅从一个文件中的某些特定行中减去另一列中的一列中的值

时间:2018-12-07 16:20:07

标签: awk

我有一个文件,其中包含不同IP地址的记录。对于第29列所示的一个特定IP地址192.168.0.13,我想减去第29列的值。从第2列中的值开始选择40(即时间= 25.101)。

即25.1796减去25.101。

在进行上述减法之前,应删除第40列中的“ time =”一词。

我希望对所有仅包含IP地址192.168.0.13的行进行减法。
跟踪文件粘贴在下面。

t 25.1765 ns3::WifiMacHeader (CTL_ACK Duration/ID=0us, RA=00:00:00:00:00:1c) ns3::WifiMacTrailer ()
r 25.178 ns3::WifiMacHeader (QOSDATA ) ns3::LlcSnapHeader (type 0x800) ns3::Ipv4Header (tos 0x0 DSCP Default ECN Not-ECT ttl 64 id 1598 protocol 17 offset (bytes) 0 flags [none] length: 284 192.168.0.35 > 1.0.0.2) ns3::UdpHeader (length: 264 49153 > 9) ns3::SeqTsHeader ((seq=1598 time=25.1142)) Payload (size=244) ns3::WifiMacTrailer ()
t 25.1782 ns3::WifiMacHeader (CTL_ACK Duration/ID=0us, RA=00:00:00:00:00:25) ns3::WifiMacTrailer ()
r 25.1796 ns3::WifiMacHeader (QOSDATA ) ns3::LlcSnapHeader (type 0x800) ns3::Ipv4Header (tos 0x0 DSCP Default ECN Not-ECT ttl 64 id 1597 protocol 17 offset (bytes) 0 flags [none] length: 284 192.168.0.13 > 1.0.0.2) ns3::UdpHeader (length: 264 49153 > 9) ns3::SeqTsHeader ((seq=1597 time=25.101)) Payload (size=244) ns3::WifiMacTrailer ()
t 25.1798 ns3::WifiMacHeader (CTL_ACK Duration/ID=0us, RA=00:00:00:00:00:0f) ns3::WifiMacTrailer ()
r 25.1816 ns3::WifiMacHeader (QOSDATA ) ns3::LlcSnapHeader (type 0x800) ns3::Ipv4Header (tos 0x0 DSCP Default ECN Not-ECT ttl 64 id 1588 protocol 17 offset (bytes) 0 flags [none] length: 284 192.168.0.29 > 1.0.0.2) ns3::UdpHeader (length: 264 49153 > 9) ns3::SeqTsHeader ((seq=1588 time=24.9821)) Payload (size=244) ns3::WifiMacTrailer ()
t 25.1817 ns3::WifiMacHeader (CTL_ACK Duration/ID=0us, RA=00:00:00:00:00:1f) ns3::WifiMacTrailer ()
r 25.1856 ns3::WifiMacHeader (QOSDATA ) ns3::LlcSnapHeader (type 0x800) ns3::Ipv4Header (tos 0x0 DSCP Default ECN Not-ECT ttl 64 id 1586 protocol 17 offset (bytes) 0 flags [none] length: 284 192.168.0.28 > 1.0.0.2) ns3::UdpHeader (length: 264 49153 > 9) ns3::SeqTsHeader ((seq=1586 time=24.9557)) Payload (size=244) ns3::WifiMacTrailer ()
t 25.1858 ns3::WifiMacHeader (CTL_ACK Duration/ID=0us, RA=00:00:00:00:00:1e) ns3::WifiMacTrailer ()
r 25.1877 ns3::WifiMacHeader (QOSDATA ) ns3::LlcSnapHeader (type 0x800) ns3::Ipv4Header (tos 0x0 DSCP Default ECN Not-ECT ttl 64 id 1580 protocol 17 offset (bytes) 0 flags [none] length: 284 192.168.0.37 > 1.0.0.2) ns3::UdpHeader (length: 264 49153 > 9) ns3::SeqTsHeader ((seq=1580 time=24.8764)) Payload (size=244) ns3::WifiMacTrailer ()
t 25.1878 ns3::WifiMacHeader (CTL_ACK Duration/ID=0us, RA=00:00:00:00:00:27) ns3::WifiMacTrailer ()
r 25.1898 ns3::WifiMacHeader (QOSDATA ) ns3::LlcSnapHeader (type 0x800) ns3::Ipv4Header (tos 0x0 DSCP Default ECN Not-ECT ttl 64 id 1597 protocol 17 offset (bytes) 0 flags [none] length: 284 192.168.0.5 > 1.0.0.2) ns3::UdpHeader (length: 264 49153 > 9) ns3::SeqTsHeader ((seq=1597 time=25.101)) Payload (size=244) ns3::WifiMacTrailer ()
t 25.1899 ns3::WifiMacHeader (CTL_ACK Duration/ID=0us, RA=00:00:00:00:00:07) ns3::WifiMacTrailer ()
t 25.1906 ns3::WifiMacHeader (EXTENSION_S1G_BEACON ) ns3::S1gBeaconHeader () ns3::WifiMacTrailer ()
r 25.2005 ns3::WifiMacHeader (QOSDATA ) ns3::LlcSnapHeader (type 0x800) ns3::Ipv4Header (tos 0x0 DSCP Default ECN Not-ECT ttl 64 id 1558 protocol 17 offset (bytes) 0 flags [none] length: 284 192.168.0.24 > 1.0.0.2) ns3::UdpHeader (length: 264 49153 > 9) ns3::SeqTsHeader ((seq=1558 time=24.5857)) Payload (size=244) ns3::WifiMacTrailer ()
t 25.2007 ns3::WifiMacHeader (CTL_ACK Duration/ID=0us, RA=00:00:00:00:00:1a) ns3::WifiMacTrailer ()
r 25.2156 ns3::WifiMacHeader (QOSDATA ) ns3::LlcSnapHeader (type 0x800) ns3::Ipv4Header (tos 0x0 DSCP Default ECN Not-ECT ttl 64 id 1564 protocol 17 offset (bytes) 0 flags [none] length: 284 192.168.0.25 > 1.0.0.2) ns3::UdpHeader (length: 264 49153 > 9) ns3::SeqTsHeader ((seq=1564 time=24.665)) Payload (size=244) ns3::WifiMacTrailer ()
t 25.2157 ns3::WifiMacHeader (CTL_ACK Duration/ID=0us, RA=00:00:00:00:00:1b) ns3::WifiMacTrailer ()
r 25.219 ns3::WifiMacHeader (QOSDATA ) ns3::LlcSnapHeader (type 0x800) ns3::Ipv4Header (tos 0x0 DSCP Default ECN Not-ECT ttl 64 id 1581 protocol 17 offset (bytes) 0 flags [none] length: 284 192.168.0.38 > 1.0.0.2) ns3::UdpHeader (length: 264 49153 > 9) ns3::SeqTsHeader ((seq=1581 time=24.8896)) Payload (size=244) ns3::WifiMacTrailer ()

trace file

1 个答案:

答案 0 :(得分:0)

请您尝试一次(严格考虑您的实际Input_file与所示示例相同)。

awk '$29=="192.168.0.13"{gsub(/.*=|\)\)/,"",$40);print $2-$40}'  Input_file

上述代码的解释:

awk '                      ##Starting awk program here.
$29=="192.168.0.13"{       ##Checking condition if 29th field is same as IP address mentioned by OP if yes then do following.
  gsub(/.*=|\)\)/,"",$40)    ##Using gsub for substituting everything till = AND )) string with NULL here in 40th field since it has value like time=25.101)) and we need only digits to subtract its value.
  print $2-$40             ##Now printing subtraction of $2 and $40 here.
}'  Input_file             ##Mentioning Input_file name here.