Bash scripting problem – compare two lists and create a third

bash

I've got the beginnings of a script that I'm putting together to check Xen hosts… this question is twofold really. Firstly I've got the below code snippet;

TMPFILE001=/tmp/FILE001.rx
TMPFILE002=/tmp/FILE002.rx
TMPFILE003=/tmp/FILE003.rx

xe vm-list params=uuid,is-control-domain | grep -B1 true | grep uuid | awk {'print $5'} > $TMPFILE001
xe vm-list params=uuid --minimal | tr ',' '\n' > $TMPFILE002

So this gives me two lists, I want to remove anything that appears in FILE002.rx from appearing in FILE001.rx and output that to FILE003.rx.

Ideally I'd not have any files used in this at all but I was struggling to get it working by trying to capture them in variables.

So if possible it would be better to have the script run, compare the output of the two commands and only show the remainder once the output of command 2 has been taken away from command 1.

Output of command 1;

cat /tmp/FILE001.rx 
468190e5-c78b-4337-8094-20638353b470
5926bbf3-c48c-4c2a-8113-d7e58520cfe7
3f98ee14-5e60-4e9b-852a-f924ffe80791

Output of command 2 (trimmed);

cat /tmp/FILE002.rx | head -5
37ae6b9e-8a00-ab10-7e17-3d70ab275b9b
d8208537-0d69-1332-9301-4699d194430f
1cf06668-5450-4d44-a7ba-c2c7b6bcd6b2
7d11c6e3-48ef-0e5e-c6ed-e8f2548533fd
7d3d0ba3-a6d6-d2a7-99f0-06fa74e625fa

Best Answer

Look at the comm command. Take the following two files

f1.txt

item1
item2
item3
item4
item5
item6

f2.txt

item1
item2
item22
item3
item4

Output

$ comm -23 f1.txt f2.txt
item5
item6

man page entry for Comm

Related Question