[英]How to match only IP Address from two files; but leaving a specific text intact in bash
I have two files;我有两个文件; can considered it as columns;
可以将其视为列; but I need to exctract only the one that has ip address.
但我只需要提取具有 IP 地址的那个。 If in both files there is text file;
如果在两个文件中都有文本文件; then can leave that intact.
然后可以保持原样。 I tried "join" and awk without success.. Here is the output and the desired output.
我尝试了“加入”和 awk 但没有成功。这是输出和所需的输出。
FILE1 FILE2 Desired Output (FILE 3)
192.168.217.36 d0:d3:e0:cb:dc:f4 192.168.217.36
192.168.229.70 20:4c:03:59:ed:6a 192.168.229.70
No_IP_Found 192.168.197.181 192.168.197.181
192.168.230.81 192.168.230.81 192.168.230.81
No_IP_Found No_IP_Found No_IP_Found
No_IP_Found 84:d4:7e:cf:5c:9c No_IP_Found
Using paste
because I'm lazy:使用
paste
,因为我很懒:
paste FILE1 FILE2 |
awk '
{
if (match($0,/[0-9]{1,3}(\.[0-9]{1,3}){3}/))
print substr($0,RSTART,RLENGTH)
else
print "No_IP_Found"
}
'
192.168.217.36
192.168.229.70
192.168.197.181
192.168.230.81
No_IP_Found
No_IP_Found
Assumptions:假设:
file1
file1
打印一个One awk
idea:一个
awk
的想法:
awk '
BEGIN { regex="([0-9]{1,3}.){3}[0-9]{1,3}" } # define the (ipv4) ip address regex
FNR==NR { a[FNR]=$1; next } # 1st file: save field #1, skip to next input line
{ if (a[FNR] ~ regex) print a[FNR] # 2nd file: if 1st file had valid ip address then print it
else if ($1 ~ regex) print $1 # else if 2nd file has valid ip address then print it
else print "No_IP_Found" # else print default
}
' file1 file2
This generates:这会产生:
192.168.217.36
192.168.229.70
192.168.197.181
192.168.230.81
No_IP_Found
No_IP_Found
$ awk '{$1~/\./?$0=$1:($2~/\./?$0=$2:$0="NO_IP_Found")}1' <(paste FILE1 FILE2)
Assuming that you only have ip addresses, mac addresses and the string NO_IP_Found as text假设您只有 ip 地址、mac 地址和字符串 NO_IP_Found 作为文本
awk '
{
$1 ~ /\./ ? $0 = $1 \ # check if field 1 has any dot "."
# if true assign $1 to $0
: # if false
( \
$2 ~ /\./ ? $0 = $2 \ # check if field 2 has any dot "."
# if true assign $2 to $0
: \ # if false
$0 = "NO_IP_Found" \ # assign "NO_IP_Found" to $0
)
}1 # print $0 after modifiying
' <(paste FILE1 FILE2)
# without assigning
$ awk '{print($1~/\./?$1:($2~/\./?$2:"NO_IP_Found"))}' <(paste FILE1 FILE2)
if you don't care about sorted order, here's a strange way to go it that filters out either ipv6 or MAC addresses but doesn't verify the ipv4 ones :如果您不关心排序顺序,这是一种奇怪的方法,可以过滤掉 ipv6 或 MAC 地址,但不验证 ipv4 的地址:
{m,g}awk '___[$_]++<(NF*=NF==!_) || (__==$_)==(FNR-___[__])' \
__='No_IP_Found' FS=':' file1 file2
192.168.217.36
192.168.229.70
No_IP_Found
192.168.230.81
192.168.197.181
No_IP_Found
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.