【问题标题】:AWK Compare two files and print matching columns with IP addressesAWK 比较两个文件并打印带有 IP 地址的匹配列
【发布时间】:2020-10-05 14:57:40
【问题描述】:

我不喜欢寻求帮助,而且大多数情况下都能解决问题;但是我无法理解我做错了什么。

有两个文件[出于明显原因已编辑]:

第一个文件;是要“查找”的 IP 列表。

$OUT

1.1.1.1
2.2.2.2
111.111.111.111



第二个文件;是用于扫描 IP 并获取 ClientUID 的日志文件。

$文件

[0513.65] DevNet: Join succeeded: FoObAr playerid=0x0000000000000000
[0522.25] NetComeGo: Close IpNetDriverSteamworks_0 IpNetConnectionSteamworks_2 111.111.111.111:12345
[0522.25] DevOnline: EndRemoteClientAuthSession: ClientAddr: 111.111.111.111:12345, ClientUID: 00000000000000000
[0522.25] DevOnline: EndLocalServerAuthSession: ClientAddr: 111.111.111.111, ClientUID: 00000000000000000
[0522.25] DevOnline: EndLocalServerAuthSession: SessionUID: 4

所需的输出:

ClientAddr: 111.111.111.111 ClientUID: 00000000000000000

我试过这个:

awk --posix 'NR==FNR{a[NR]=$1;next}{for (i in a){if($4 ~ /^ClientAddr/ && $5 ~ /"^"a[i]/)print $4 $5 $6 $7}}' $OUT $file

还有

awk --posix 'NR==FNR{a[$1];next}{for (i in a){if($4 ~ /^ClientAddr/ && $5 ~ /"^"a[i]/)print $4 $5 $6 $7}}' $OUT $file

但它不起作用;在过去的几天里,我一直在阅读和查找信息....但我无法理解我做错了什么。


完整的 bash 脚本:

#!/bin/bash

file=$1
OUT=/tmp/scanLog/data.tmp

awk --posix '$2 ~ /^NetComeGo/ && $5 ~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/ {a[$5]++}END{for(i in a)if(a[i] > 100){print i}}' $file >$OUT

awk --posix 'NR==FNR{a[NR]=$1;next}{for (i in a){if($4 ~ /^ClientAddr/ && $5 ~ /"^"a[i]/)print $4 $5 $6 $7}}' $OUT $file

rm $OUT

我目前的心理健康有问题;我希望这个帮助请求是有意义的。


【问题讨论】:

  • 为什么想要的输出只有一行?
  • 运行时得到什么输出?哪方面不正确?
  • 您对在 AWK 中执行此操作的依附程度如何?在 Perl 或 Python 中可能更简单。
  • Perl 可能是一个替代方案。

标签: awk


【解决方案1】:

假设您需要检查 Input_file2 中与 Input_file1 中找到的 IP 地址相同的行关注。

awk '
FNR==NR{
  a[$0]
  next
}
/ClientUID:/ && match($0,/ClientAddr: [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+/){
  val=substr($0,RSTART+12,RLENGTH-12)
  sub(/:.*/,"",val)
  if(val in a){
    print "ClientAddr:",val,$(NF-1),$NF
  }
}
'  Input_file1  Input_file2

说明:为上述添加详细说明。

awk '                                              ##Starting awk program from here.
FNR==NR{                                           ##Checking condition FNR==NR which will be TRUE when Input_file1 is being read.
  a[$0]                                            ##Creating array a with index current line here.
  next                                             ##next will skip all further statements from here.
}
/ClientUID:/ && match($0,/ClientAddr: [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+/){    ##Checking condition if ClientUID is present in line and ClientAddr with IP addrsss then colon digits are present in current line then do following.
  val=substr($0,RSTART+12,RLENGTH-12)              ##Creating variable val which has sub-string of current line
  sub(/:.*/,"",val)                                ##Substituting from colon to everything till last with NULL in val to get only IP address.
  if(val in a){                                    ##Checking if val is present in a then do following.
    print "ClientAddr:",val,$(NF-1),$NF            ##Printing string ClientAddrthen val then last 2 fields of line here as per shown samples.
  }
}
'  file1 file2                                     ##Mentioning Inpupt_file names here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-30
    • 2020-12-13
    • 2021-06-15
    • 2020-04-16
    • 2016-10-23
    • 2017-07-30
    • 1970-01-01
    • 2020-11-24
    相关资源
    最近更新 更多