Awk 对于一个文件中的某些特定行,从另一列中减去一列中的值

Awk 对于一个文件中的某些特定行,从另一列中减去一列中的值,awk,Awk,我有一个包含不同IP地址记录的文件。对于第29列显示的一个特定IP地址192.168.0.13,我想从第2列的值中减去第40列的值(即时间=25.101) i、 e 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) ns

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

i、 e 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 ()

请您尝试一下以下内容(严格考虑到您的实际输入文件与所示示例相同)

上述代码说明:


欢迎来到SO,请在代码标签的文本文件中发布您的样本,然后让我们知道。欢迎来到Stack Overflow!对不起,这不是StackOverflow的工作方式。形式为“我想做X,请给我提示和/或示例代码”的问题被认为是离题的。请访问并阅读,特别是阅读
awk'($29==“192.168.0.13”){print substr($40,6,length($40)-5-2)-$2}
您能告诉我在上述解决方案中-5-2的用途吗。字段40看起来像
time=25.101))
,the
substr(str,n,m)
命令从长度为
m
的字符
n
开始的
str
中选取子字符串。字符串
time=
为5个字符,两个括号为2。所以这个数字实际上是
长度($40)-5-2
长度,从位置开始6@Jan,请检查上面的代码,并告诉我这是否对您有帮助?它给出了“正则表达式编译失败(缺少操作数。*=|”)”@Jan,好的,我不确定它是否对我有效。现在试试我编辑的代码,我对它做了一点修改,然后告诉我?正则表达式编译失败。@Jan,我不确定它是否正常工作,请您通过执行
cat-v输入文件
检查您的输入文件是否具有控制权?如果是,则尝试通过执行
tr-d'\r'temp\u file&&mv temp\u file Input\u 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.