3

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
  • (quick suggestion) check grep manpage for -F. Not posting as an answer as its just a lead... – derobert Dec 2 '14 at 19:37
  • Thanks, that got me to where I need to be. I'd still be happy to see a more elegant solution though :). – Chris Davidson Dec 2 '14 at 19:50
  • Feel free to post how you did it as an answer (to help anyone in the future with a similar problem). – derobert Dec 2 '14 at 19:51
5

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

  • comm is exactly the tool for this job. – DopeGhoti Dec 2 '14 at 21:37
  • you want to sort this first, I think. – mikeserv Dec 11 '14 at 18:46
1

To answer one part of the question, you can use grep with the -F flag to compare the difference between two files. I then combined this with -v to take the difference away for what I required;

grep -vxFf $TMPFILE001 $TMPFILE002 > $TMPFILE003
  • add -x and -F to the grep options, otherwise you risk "false" matches. – glenn jackman Dec 2 '14 at 20:02
1

Or if you prefer you can do this:

diff file1 file2 | grep "<" | sed 's/^<\ //g' > file3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.