7

I'm trying to compute the difference between the output of two awk commands but my simple attempts at it seem to be failing. Here is what I'm trying:

diff $(awk '{print $3}' f1.txt | sort -u) $(awk '{print $2}' f2.txt | sort -u)

This doesn't work for reasons unknown to me. I was under the assumption that $() construct was used to capture the output of another command but my "diff" invocation fails to recognize the two inputs given to it. Is there any way I can make this work.

By the way, I can't use the obvious solution of writing the output of those two commands to separate files given that I'm logged on to a production box with no 'write' privileges.

2
  • try writing output of both awk functions into different files and compare them using diff command Dec 21, 2011 at 18:26
  • Unfortunately that isn't acceptable since I'm logged on to a production box with no write access.
    – sasuke
    Dec 21, 2011 at 18:38

1 Answer 1

23

diff expects the names of two files, so you should put the two output on two files, then compare them:

awk '{print $3}' f1.txt | sort -u > out1
awk '{print $2}' f2.txt | sort -u > out2
diff out1 out2

or, using ksh93, bash or zsh, you can use process substitution:

diff <(awk '{print $3}' f1.txt | sort -u) <(awk '{print $2}' f2.txt | sort -u)
5
  • Wow, the second solution works like a charm (unfortunately can't use first since I can't create a file on that box). BTW, can you throw some pointers/links on what kind of black magic this <() construct is and what is it called?
    – sasuke
    Dec 21, 2011 at 18:17
  • @sasuke: See this other answer: unix.stackexchange.com/questions/22645/…
    – enzotib
    Dec 21, 2011 at 18:59
  • 2
    @sasuke you don't need to put outX in the same dir, you could put them in ~/ or /tmp/. You should have some place on the machine you could create tmp files even on a "production box".
    – Johan
    Dec 22, 2011 at 6:23
  • @Johan: Thanks for the comment. Unfortunately, the "production" box is a bit borked when it comes to configuration so all developers have strictly read only rights (yes, even home directories, sad but true).
    – sasuke
    Dec 23, 2011 at 18:50
  • @sasuke /tmp should still be writable though, or the system is truly borked.
    – Kusalananda
    Feb 3, 2019 at 19:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .