Problem:
Illustration by example
Suppose, The two files are test1 and test2.
$ cat test2
www.xyz.com/abc-2
www.xyz.com/abc-3
www.xyz.com/abc-4
www.xyz.com/abc-5
www.xyz.com/abc-6
And test1 is
$ cat test1
www.xyz.com/abc-1
www.xyz.com/abc-2
www.xyz.com/abc-3
www.xyz.com/abc-4
www.xyz.com/abc-5
Comparing test1 to test2 and removing duplicates from test 1
Result Required:
$ cat test1
www.xyz.com/abc-1
and then adding this test1 data in to test2
$ cat test2
www.xyz.com/abc-2
www.xyz.com/abc-3
www.xyz.com/abc-4
www.xyz.com/abc-5
www.xyz.com/abc-6
www.xyz.com/abc-1
Solutions Tried:
join -v1 -v2 <(sort test1) <(sort test2)
which resulted into this (that was wrong output)
$ join -v1 -v2 <(sort test1) <(sort test2)
www.xyz.com/abc-1
www.xyz.com/abc-6
Another solution i tried was :
fgrep -vf test1 test2
which resulted nothing.
Remove lines from test1 because they are in test2:
$ grep -vxFf test2 test1
www.xyz.com/abc-1
To overwrite test1:
grep -vxFf test2 test1 >test1.tmp && mv test1.tmp test1
To append the new test1 to the end of test2:
cat test1 >>test2
grep normally prints matching lines. -v
tells grep to do the reverse: it prints only lines that do not match
-x
tells grep to do whole-line matches.
-F
tells grep that we are using fixed strings, not regular expressions.
-f test2
tells grep to read those fixed strings, one per line, from file test2.
With awk:
% awk 'NR == FNR{ a[$0] = 1;next } !a[$0]' test2 test1
www.xyz.com/abc-1
Breakdown:
NR == FNR { # Run for test2 only
a[$0] = 1 # Store whole line as key in associative array
next # Skip next block
}
!a[$0] # Print line from test1 that are not in a
Solution to 1 and 2 problem.
diff test1 test2 |grep "<"|sed 's/< \+//g' > test1.tmp|mv test1.tmp test1
here is the output
$ cat test1
www.xyz.com/abc-1
solution to 3 problem.
cat test1 >> test2
here is the output
$ cat test2
www.xyz.com/abc-2
www.xyz.com/abc-3
www.xyz.com/abc-4
www.xyz.com/abc-5
www.xyz.com/abc-6
www.xyz.com/abc-1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With