Bash 如何使用AWK或GREP比较两个文件
所以我有两个问题,我有两个文件,我试图排序和过滤。在这两个文件中,每个文件都有两列,其中file1有IP和Port,file2有domain和IPBash 如何使用AWK或GREP比较两个文件,bash,awk,grep,Bash,Awk,Grep,所以我有两个问题,我有两个文件,我试图排序和过滤。在这两个文件中,每个文件都有两列,其中file1有IP和Port,file2有domain和IP file1: Address,Port 1.2.3.4,8080 4.5.6.7,80 6.7.8.9,443 file2: Domain,IP google.com,1.2.3.4 google.fe,6.7.8.9 admin.ko,3.2.4.5 所以第一个问题是:我想在文件1中找到与文件2中的任何IP都不匹配的IP 我尝试过使用awk
file1:
Address,Port
1.2.3.4,8080
4.5.6.7,80
6.7.8.9,443
file2:
Domain,IP
google.com,1.2.3.4
google.fe,6.7.8.9
admin.ko,3.2.4.5
所以第一个问题是:我想在文件1中找到与文件2中的任何IP都不匹配的IP
我尝试过使用awk,以下是我使用的:
awk -F',' FNR==NR{ a[$2]; next } !($1 in a)' file2 file1
所以我真的不太理解awk,所以有人能帮我理解您提供的awk命令的每一部分吗:)
下一个问题,我不知道怎么做,所以请帮忙
第二个问题:所以我想列出与第一个相同的期望输出,但这次我想添加domain列
Desired output:
Address,Port,Domain,Status
1.2.3.4,8080,google.com,Present
4.5.6.7,80,NULL,Not-Present
6.7.8.9,443,google.fe,Present
提前谢谢。以下是您提到的代码的完整解释,请仔细阅读
awk -F',' ' ##Setting awk program here and setting comma as field separator for all lines here.
FNR==NR{ ##Checking condition if FNR==NR which will be TRUE when first Input_file named file2 is being read.
a[$2] ##Creating an array named a with index $2 of current line here.
next ##next will skip all further statements from here.
}
!($1 in a) ##Checking condition if $1 is NOT present from Input_file1 then print that line from Input_file1.
' file2 file1 ##Mentioning Input_file names here.
对于第二个问题,您可以尝试以下代码
awk '
BEGIN{
FS=OFS=","
}
FNR==1{
if(++count==1){
val=$0
}
if(++count==2){
print val,$1,"Status"
}
next
}
FNR==NR{
a[$2]=$1
next
}
{
print $0,$1 in a?"Present":"Not-Present"
}
' file2 file1
以下是您提到的代码的完整解释,请仔细阅读
awk -F',' ' ##Setting awk program here and setting comma as field separator for all lines here.
FNR==NR{ ##Checking condition if FNR==NR which will be TRUE when first Input_file named file2 is being read.
a[$2] ##Creating an array named a with index $2 of current line here.
next ##next will skip all further statements from here.
}
!($1 in a) ##Checking condition if $1 is NOT present from Input_file1 then print that line from Input_file1.
' file2 file1 ##Mentioning Input_file names here.
对于第二个问题,您可以尝试以下代码
awk '
BEGIN{
FS=OFS=","
}
FNR==1{
if(++count==1){
val=$0
}
if(++count==2){
print val,$1,"Status"
}
next
}
FNR==NR{
a[$2]=$1
next
}
{
print $0,$1 in a?"Present":"Not-Present"
}
' file2 file1
1) 这是非常接近你原来的尝试。。。我们只是根据FNR添加了不同的第3列
注:
- 报头从不包含IP,因此将其存储在[]中并没有真正的区别
- 脚本末尾的1很重要,副作用会打印到标准输出
a[]
来存储域,并相应地修改第3列和第4列
awk -F, -v OFS=, '
FNR == NR { a[$2] = $1; next }
FNR == 1 { $3 = "Domain"; $4 = "Status" }
FNR != 1 {
if ($1 in a) {
$3 = a[$1]; $4 = "Present"
} else {
$3 = "NULL"; $4 = "Not-Present"
}
}
1' file2 file1
1) 这是非常接近你原来的尝试。。。我们只是根据FNR添加了不同的第3列
注:
- 报头从不包含IP,因此将其存储在[]中并没有真正的区别
- 脚本末尾的1很重要,副作用会打印到标准输出
a[]
来存储域,并相应地修改第3列和第4列
awk -F, -v OFS=, '
FNR == NR { a[$2] = $1; next }
FNR == 1 { $3 = "Domain"; $4 = "Status" }
FNR != 1 {
if ($1 in a) {
$3 = a[$1]; $4 = "Present"
} else {
$3 = "NULL"; $4 = "Not-Present"
}
}
1' file2 file1
请尽量保持一个问题一条线,以避免任何形式的混淆。请尽量保持一个问题一条线,以避免任何形式的混淆。