awk - 比较两个文件中的列并替换第一个文件中的文本

时间:2017-11-10 19:39:15

标签: shell gawk

我有两个文件。第一列有1列,第二列有3列。我想比较两个文件的第一列。如果有巧合,请将第2列和第3列替换为特定值;如果没有,请打印相同的行。

文件1:

$ cat file1
26
28
30

文件2:

$ cat file2
1,a,0
2,a,0
22,a,0
23,a,0
24,a,0
25,a,0
26,r,1510139756
27,a,0
28,r,1510244156
29,a,0
30,r,1510157364
31,a,0
32,a,0
33,r,1510276164
34,a,0
40,a,0

期望的输出:

$ cat file2
1,a,0
2,a,0
22,a,0
23,a,0
24,a,0
25,a,0
26,a,0
27,a,0
28,a,0
29,a,0
30,a,0
31,a,0
32,a,0
33,r,1510276164
34,a,0
40,a,0

我正在使用gawk执行此操作(它在shell脚本中,我使用的是solaris),但我无法正确输出。它只打印匹配的行:

$fuente="file2"
gawk -v fuente="$fuente" 'FNR==NR{a[FNR]=$1; next}{print $1,$2="a",$3="0" }' $fuente file1 > file3

我得到的输出:

$ cat file3
26 a 0
28 a 0
30 a 0

2 个答案:

答案 0 :(得分:0)

为了清晰起见,真的很分散;叫(fuente.awk)就像这样:

    awk -F \, -v fuente=file1 -f fuente.awk file2  # -F == IFS


    BEGIN {
        OFS=","          # set OFS to make printing easier

        while (getline x < fuente > 0)   # safe way; read file into array
        {
            a[++i]=x     # stuff indexed array
        }
    }
    {   # For each line in file2
        for (k=1 ; k<=i ; k++)   # Lop over array (elements in file1)
        {
            if (($1==a[k]) && (! flag))
            {
                print($1,"a",0)    # Found print new line
                flag=1             # print only once
            }
        }

        if (! flag)     # Not found
        {
            print($0)   # print original
        }

        flag=0          # reset flag
    }
    END { }

答案 1 :(得分:0)

awk 单行

awk 'NR==FNR{ a[$1]; next }$1 in a{ $2="a"; $3=0 }1' file1 FS=',' OFS=',' file2

输出:

1,a,0
2,a,0
22,a,0
23,a,0
24,a,0
25,a,0
26,a,0
27,a,0
28,a,0
29,a,0
30,a,0
31,a,0
32,a,0
33,r,1510276164
34,a,0
40,a,0