比较文件与AWK [英] Compare files with AWK

查看:123
本文介绍了比较文件与AWK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有两个类似的文件(包括3列)。我想,以检查是否这两个文件中包含的相同的元件(但在不同的订单中列出)。首先我想只比较第一列

Hi I have two similar files (both with 3 columns). I'd like to check if these two files contains the same elements (but listed in a different orders). First of all I'd like to compare only the 1st columns

FILE1.TXT

file1.txt

"aba" 0 0 
"abc" 0 1
"abd" 1 1 
"xxx" 0 0

FILE2.TXT

file2.txt

"xyz" 0 0
"aba" 0 0
"xxx" 0 0
"abc" 1 1

我如何能做到用awk?我想看看周围,但我发现只有复杂的例子。如果我想也包括了比较其他两列?输出应给我匹配元素的数量。

How can I do it using awk? I tried to have a look around but I've found only complicate examples. What if I want to include also the other two columns on the comparison? The output should give me the number of matching elements.

推荐答案

要打印普通的元素的两个文件:

To print the common elements in both files:

$ awk 'NR==FNR{a[$1];next}$1 in a{print $1}' file1 file2
"aba"
"abc"
"xxx"

说明:

NR FNR 是存储 AWK 变量记录的总数和记录在当前的文件分别数(默认记录是一行)。

NR and FNR are awk variables that store the total number of records and the number of records in the current files respectively (the default record is a line).

NR==FNR # Only true when in the first file 
{
    a[$1] # Build assicioative array on the first column of the file
    next  # Skip all proceeding blocks and process next line
}
($1 in a) # Check in the value in column one of the second files is in the array
{
    # If so print it
    print $1
}

如果您要匹配的整行,然后使用 $ 1,0

If you want to match the whole lines then use $0:

$ awk 'NR==FNR{a[$0];next}$0 in a{print $0}' file1 file2
"aba" 0 0
"xxx" 0 0

或特定的一组列:

$ awk 'NR==FNR{a[$1,$2,$3];next}($1,$2,$3) in a{print $1,$2,$3}' file1 file2
"aba" 0 0
"xxx" 0 0

这篇关于比较文件与AWK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆