-
Notifications
You must be signed in to change notification settings - Fork 25
/
diff-2
executable file
·45 lines (43 loc) · 1.06 KB
/
diff-2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/bash
set -euf -o pipefail
##
# Print lines that are only in file 2.
#
# Example:
#
# cat 1.txt
# a
# b
#
# cat 2.txt
# b
# c
#
# diff-2 a.txt b.txt
# c
#
# Credit: http://stackoverflow.com/questions/4717250/extracting-unique-values-between-2-sets-files
#
# This script is logically the same as:
#
# comm -13 <(sort 1.txt) <(sort 2.txt)
#
# This script is faster than comm (especially on large files)
# because this script doesn't need the inputs to be sorted.
#
# Explanation:
#
# * FNR is the current file's record number
# * NR is the current overall record number
# * FNR==NR is true when we are reading file1, not file2
# * $0 is the line text
# * a[$0] is a hash that tracks that we've seen the line text
# * FNR==NR{a[$0];next} tracks that we've seen all the file1 line text
# * !($0 in a) is true when we have not seen the line text
# * awk default is to print the line text
#
# Contact: Joel Parker Henderson ([email protected])
# License: GPL
# Updated: 2015-01-25
##
awk 'FNR==NR{a[$0];next}!($0 in a)' "$@"