I have a text file with two columns and I want to print only the strings that are present in both of them. For example:

column1       column2

stringA       stringZ
stringP       stringT
stringZ       stringX
stringE       stringR
stringT       stringG

Expected output:


Shamelessly stolen from @cherdt with some improvement (assumes a shell like zsh or bash with support for ksh-like process substitution):

f=filename; comm -12 <(cut -f1 < "$f" |sort) <(cut -f2 < "$f" | sort)
  • Keeping filename in variable helps not repeat it
  • No need to write to files, then to compare. Writing to files usually requires to delete them afterwards for cleanup. Don't do this with huge files though. Process substitution makes it look like comm is reading from files whereas it's stdout redirection to a temporary fd
| improve this answer | |

With different awk solution and easy

awk 'NR==FNR{seencol1[$1]++;next} ($2 in seencol1)' infile.txt infile.txt 


| improve this answer | |

You can use comm to compare 2 files, so first generate a sorted file for each column:

  1. cat filename | cut -f1 | sort > column1
  2. cat filename | cut -f2 | sort > column2

Then suppress the first 2 columns of comm to display only the rows where the data exists in both files:

comm -12 col1 col2

| improve this answer | |
awk 'NF!=1{cola[$1]++; colb[$2]++} END { for (item in cola ) { if( colb[item]>0 ) { print item } }' /path/to/input
| improve this answer | |

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.