Search code examples
bashshellawkcutcomm

Taking a Column of Output from comm Without Losing Blank Lines


I'm trying to assemble a list of software installations per user. I have the complete list of users in file1.txt, and the list of users with the software installed in file2.txt. I want to end up with lines that are only in file1.txt or in both file1.txt and file2.txt, but including blank lines. So:

file1.txt

bhope
bsmith
fjones
jdoe
zdirks

file2.txt

bhope
jdoe
zdirks

I can get partway there with comm -2 file1.txt file2.txt, which gives me:

<tab>bhope
bsmith
fjones
<tab>jdoe
<tab>zdirks

I want the output to be:

bhope
<blank line>
<blank line>
jdoe
zdirks

With bonus points for replacing the actual output lines with "Y"'s:

Y
<blank line>
<blank line>
Y
Y

But can't find a way to preserve the blank lines with cut or awk.


Solution

  • Assuming you have fixed the sorted problem:

    comm -2 file1.txt file2.txt | awk -F'\t' '{print (NF==2?"y":"")}'
    
    y
    
    
    y
    y