-2

I have 3 coumns of genes in a file like this

col1    col2    col3
CXCL9   CXCL9   CXCL9
MAP2K6  MAP2K6  MAP2K6
CXCL10  CXCL10  CXCL11

I want to match the 3 columns and see which gene is present in how many columns, The output desired is in the format as

CXCL9 3
MAP2K6 3
CXCL10 2
CXCL11 1

Can somebody help me, it would save me a lot of time

1

sed + sort + uniq solution:

sed 's/[[:space:]]\+/\n/g' file | sort | uniq -c

The output:

  2 CXCL10
  1 CXCL11
  3 CXCL9
  3 MAP2K6
  • What about the col1, col2, col3 headings? – Mukesh Sai Kumar Jan 13 '18 at 16:24
  • @MukeshSaiKumar, I suppose that they were just for demonstration purpose – RomanPerekhrest Jan 13 '18 at 16:25
  • . . . perhaps sed -n '1!s/[[:space:]]\+/\n/gp' ? – steeldriver Jan 13 '18 at 16:26
0

If there are no spaces between the names of genes, and the names of the columns follow the pattern you gave, then you may use this following script as a hint:

#!/bin/bash
for i in `cat genes.txt`; do
    [[ $i == "col"* ]] || echo $i;
done | sort | uniq -c
0

Awk solution:

awk '{ for(i=1;i<=NF;i++) a[$i]++ }END{ for(i in a) print i, a[i] }' file

The output:

CXCL11 1
MAP2K6 3
CXCL9 3
CXCL10 2

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.