3

I have a file like this:

A1-1915435_1_MIMAT0000069_MirBase_miR-16_microRNA_1_22-A1-1915435_1_ENSG00000206737_ENST00000384010_U1_snRNA_73_115
A1-2362643_1_MIMAT0000732_MirBase_miR-378_microRNA_1_21-A1-2362643_1_ENSG00000222328_ENST00000410396_U2_snRNA_168_211

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.

1
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.*//'

...or...

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.

6
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.

3

Using sed:

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

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 '14 at 10:01

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.