I have a file like this:


I want an output in which I get micro RNA part and ENSG part tab separated. Some thing like this:

miR-16_microRNA ENSG00000206737
miR-378_microRNA    ENSG00000222328

I can write a perl script for it but I guess there must be command line solution for this using awk, sed perl etc.

4 Answers 4

perl -pe 's!.*(miR-\d+_microRNA).*(ENSG\d+).*!$1\t$2!'

explain a bit?

Sure, sorry:

  • for each line (perl -p)
  • substitute regex by $1 tab $2 where
    • $1 and $2 are numbered backreferences to capturing groups (...). Nearly all modern regular expression engines support this.
  • about the regular expression:
    • . = any char except \n
    • .* = a sequence of chars (ignored in the substitution)
    • \d = digit (the same as [0-9])
    • \d+ = one or more \d

Please see any reg expression tutorial for a more detailed explanation.


Using sed:

sed 's/^.*\(miR-[0-9]*_microRNA\).*\(ENSG[0-9]*\).*$/\1\t\2/' <infile

Using assuming the format is reliable :

$ awk -F'_' '{print $5"_"$6, $11}' file
miR-16_microRNA ENSG00000206737
miR-378_microRNA ENSG00000222328
  • 1
    why not awk -F'_' '{print $5"_"$6, $11}'
    – user78605
    Dec 31, 2014 at 10:01
sed -n 'y/_E/\t\n/;s/\n/E/;s/[^m]*//;s/\(A.\).*E/\1E/;P'

That works for me given your example...

miR-16  microRNA        ENSG00000206737
miR-378 microRNA        ENSG00000222328

If you want the first \tab converted back to a _ then you can add s/\t/_/ before the P - but I think it looks nicer like this.

Another way could look like...

sed 's/RNA[^E]*_/RNA\t/;s/[^m]*//;s/_E.*//'


sed 's/[^mE]*\([mE][^mE_]*\)/\1/;s//\t\1/3;s/_E.*//'

...which both print what you show in your desired output like...

miR-16_microRNA ENSG00000206737
miR-378_microRNA        ENSG00000222328

. In any case you might have to use a literal character in place of the \t escape depending on your sed implementation.

You must log in to answer this question.

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