问题描述:

I have two file with comma separated values, I want to remove all the strings in file1 matching with strings in file 2.

file1:

soap,cosmetics,june,hello,good

file2:

june,hello

output:

soap,cosmetics,good

I tried this, but not working. I'm not sure where I'm going wrong. Any help appreciated.

BEGIN {

FS=","

}

NR==FNR {

a[NR]=$0

next

}

{

for (j=1;j<=NF;j++) {

split($0, d, ",")

if (d[j] in a == 0) {

line = (line ? line "," : "") d[j]

}

}

print line

line = ""

}

网友答案:

Here's one way using awk. Run like:

awk -f script.awk file2 file1

Contents of script.awk:

BEGIN {
    FS=","
}

FNR==NR {
    for(i=1;i<=NF;i++) {
        a[$i]
    }
    next
}

{
    for(j=1;j<=NF;j++) {
        if (!($j in a)) {
            r = (r ? r FS : "") $j
        }
    }
}

END {
    print r
}

Results:

soap,cosmetics,good

Alternatively, here's the one-liner:

awk -F, 'FNR==NR { for(i=1;i<=NF;i++) a[$i]; next } { for(j=1;j<=NF;j++) if (!($j in a)) r = (r ? r FS : "") $j } END { print r }' file2 file1
网友答案:
$ gawk -v RS='[,\n]' 'NR==FNR{a[$0];next} !($0 in a){o=o s $0;s=","} END{print o}' file2 file1
soap,cosmetics,good
相关阅读:
Top