Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Parsing in perl?

by perllearner007 (Acolyte)
on Dec 08, 2011 at 04:30 UTC ( #942351=perlquestion: print w/ replies, xml ) Need Help??
perllearner007 has asked for the wisdom of the Perl Monks concerning the following question:


Hello. I have a text file(tab-delim) with some gene names as shown below.

BRCA1:breast cancer 1, early onset

TNF: tumor necrosis factor

OMG:oligodendrocyte myelin glycoprotein

I would like to get the list of gene name BRCA1,TNF,OMG that is before the colon(:) . How do I parse in perl this text file with this list of genes?
Thank you.

Comment on Parsing in perl?
Re: Parsing in perl?
by Anonymous Monk on Dec 08, 2011 at 05:01 UTC

    perlintro, perlrun, B::Deparse, ... parse tab ... bioperl ...

    Editing just one column in a file

    $ cat junk BRCA1:breast cancer 1, early onset TNF: tumor necrosis factor OMG:oligodendrocyte myelin glycoprotein $ perl -F: -lane " print $F[0]; " junk BRCA1 TNF OMG $ perl -MO=Deparse -F: -lane " print $F[0]; " junk BEGIN { $/ = "\n"; $\ = "\n"; } LINE: while (defined($_ = <ARGV>)) { chomp $_; our(@F) = split(/:/, $_, 0); print $F[0]; } -e syntax OK
Re: Parsing in perl?
by ansh batra (Friar) on Dec 08, 2011 at 05:08 UTC
    #! usr/bin/perl -w open(FILE,"<gene.txt"); my @lines=<FILE>; close(FILE); foreach my $line(@lines) { if($line=~/:/) { print "$`\n"; } }

    output
    BRCA1 TNF OMG

    PS: $` is a special variable used to extract data before the regular expression

      Or without the $` ( $PREMATCH ) imposed penalty ( see, depending on your shell, perldoc -v "$`" or perldoc -v '$`') , its more Modern Perl-ish

      #!usr/bin/perl -- use strict; use warnings; use autodie qw/ open close /; # error checking open my($file), '<', 'gene.txt'; while( <$file> ){ print $1 if /^([^:]+):/; } close $file;
Re: Parsing in perl?
by TJPride (Pilgrim) on Dec 08, 2011 at 13:27 UTC
    use strict; use warnings; my @list; open (FH, 'sample.txt') || die; while (<FH>) { push @list, $1 if m/^(.+?):/; } print "@list";
Re: Parsing in perl?
by planetscape (Canon) on Dec 08, 2011 at 15:38 UTC
Re: Parsing in perl?
by CountZero (Bishop) on Dec 08, 2011 at 22:48 UTC
    So many ways to do it!
    use Modern::Perl; say +(split ':')[0] while (<DATA>); __DATA__ BRCA1:breast cancer 1, early onset TNF: tumor necrosis factor OMG:oligodendrocyte myelin glycoprotein

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Parsing in perl?
by Anonymous Monk on Dec 08, 2011 at 23:44 UTC
    Thank you all for your replies.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://942351]
Approved by Marshall
Front-paged by CountZero
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-09-16 03:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (155 votes), past polls