UNIX: using comm without creating temporary files

863 Views Asked by At

I wanted to use comm to compare 2 lists: one consists of randomly generated words:

cat /dev/urandom | head -n 10000 | strings | tr 'A-Z' 'a-z' | sort

the other one is an english dictionary:

wget -q0- <URL> | sort

(I can't really give away the URL) I tried saving both lists to temporary files and then used comm -12 file1 file2 and it worked, but now i want to do it without creating those temporary files. Is there a way?

2

There are 2 best solutions below

1
On

Your code (with the useless use of cat refactored) can be trivially rewritten to use a Bash process substitution:

comm <(head -n 10000 </dev/urandom |
        strings | tr 'A-Z' 'a-z' | sort) <(wget -q0- <URL> | sort)

However, unless your goal is to expedite the heat death of the universe, your code looks massively inefficient. Perhaps you should explain what you are trying to accomplish? (Plus if you want to find the frequency of dictionary words in /dev/urandom output, I believe strings will be filtering out any really short words.)

0
On

Btw i found another solution, not using comm.

((head -n 10000 </dev/urandom | strings | tr 'A-Z' 'a-z' | sort | uniq) ; (wget -q0- <URL> | sort)) | sort | uniq -d

It's not very efficient, but it works (uniq -d only prints duplicated lines = lines in both files).