0

I have 2 files which both have 2 columns. I wish to remove lines from 1.txt if the same column is in 2.txt.

Example -

1.txt (contains)

example:test
example2:test2
example3:test3

2.txt contains (contains)

example:example
example2:example
example3:example
example4:example
example5:example

so in 1.txt on column 1 the entries are the same as 2.txt, so expected output in this scenario would be

example4:example
example5:example

removed content -

example:test
example2:test2
example3:test3
2

The problem description could be clearer, but based on the example I think you want this:

awk -F: 'NR==FNR { d[$1]=1; next } !($1 in d) { print $0 }' 1.txt 2.txt

The -F: argument says "treat the colon character ':' as the column separator". (By default awk treats runs of whitespace as the column separator.)

The second argument is an awk program that says

IF this line is from the first input file THEN
    In a dictionary named `d`, create an item whose key is the first column of the input line and whose value is 1
    Skip the rest of the program move on to process the next line

(because of the "skip to next line" above we only do this for lines from the second file)
IF the dictionary named `d` has no item whose whose key is the first column of this line THEN
    Print this line
1

Use below command works perfectly

awk 'NR==FNR {a[$0];next}($0 in a) {print $0}' 1.txt 2.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.