Working in linux/shell env, how can I accomplish the following:
text file 1 contains:
1 2 3 4 5
text file 2 contains:
6 7 1 2 3 4
I need to extract the entries in file 2 which are not in file 1. So '6' and '7' in this example.
How do I do this from the command line?
many thanks!
The 'uniq' command with the '-u' option only outputs lines that are uniq. If two or more of the same ID appear on neighboring lines in the input, nothing is output. The 'uniq' command with the '-d' flag only outputs a line if it is repeated two or more times in the input.
The uniq command in UNIX is a command line utility for reporting or filtering repeated lines in a file. It can remove duplicates, show a count of occurrences, show only repeated lines, ignore certain characters and compare on specific fields.
$ awk 'FNR==NR {a[$0]++; next} !($0 in a)' file1 file2 6 7
Explanation of how the code works:
Explanation of details:
FNR
is the current file's record numberNR
is the current overall record number from all input filesFNR==NR
is true only when we are reading file1$0
is the current line of texta[$0]
is a hash with the key set to the current line of texta[$0]++
tracks that we've seen the current line of text!($0 in a)
is true only when we have not seen the line textUsing some lesser-known utilities:
sort file1 > file1.sorted sort file2 > file2.sorted comm -1 -3 file1.sorted file2.sorted
This will output duplicates, so if there is 1 3
in file1
, but 2 in file2
, this will still output 1 3
. If this is not what you want, pipe the output from sort
through uniq
before writing it to a file:
sort file1 | uniq > file1.sorted sort file2 | uniq > file2.sorted comm -1 -3 file1.sorted file2.sorted
There are lots of utilities in the GNU coreutils package that allow for all sorts of text manipulations.
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