1

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:

stringZ
stringT

4 Answers 4

3

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
2

With different awk solution and easy

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

output,

stringZ
stringT
1

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

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

You must log in to answer this question.

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