-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathselect_lines
executable file
·56 lines (50 loc) · 1.4 KB
/
select_lines
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
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
help() {
echo 'usage: select_lines (only-left|only-right|in-both) left_file right_file'
echo ' select_lines --help'
echo
echo ' Depending on first argument will emit a list of'
echo ' lines that are either only in the left, the right'
echo ' or in both files.'
echo
echo ' (The script will create sorted temporary copies'
echo ' of both files...)'
echo
echo ' Thanks to mr.spuratic in https://stackoverflow.com/a/18205289'
echo
exit 1
}
[ "$1" == "--help" ] && help
[ "$3" == "" ] && echo "ERROR: missing parameters" && echo && help
left_sorted=$( mktemp )
right_sorted=$( mktemp )
sort "$2" > "$left_sorted"
sort "$3" > "$right_sorted"
case "$1" in
only-left)
diff --old-group-format="%<" \
--new-group-format="" \
--unchanged-group-format="" \
"$left_sorted" \
"$right_sorted"
;;
only-right)
diff --old-group-format="" \
--new-group-format="%>" \
--unchanged-group-format="" \
"$left_sorted" \
"$right_sorted"
;;
in-both)
diff --old-group-format="" \
--new-group-format="" \
--unchanged-group-format="%=" \
"$left_sorted" \
"$right_sorted"
;;
*)
echo "ERROR: unkown first parameters" && echo && help
;;
esac
rm "$left_sorted"
rm "$right_sorted"