Parsing a text file

Discussion in 'Perl Misc' started by silverfish, Feb 6, 2006.

  1. silverfish

    silverfish Guest

    Newbie here. I would like to search through a text file and extract a
    word from a sentence. For example,

    " I have 10 apples"
    " I have 20 apples"

    How can I extract the 10 and 20 from the sentence? The rest of the
    words in the sentence is always fixed.

    thanks for any help!
     
    silverfish, Feb 6, 2006
    #1
    1. Advertising

  2. silverfish

    Anno Siegel Guest

    silverfish <> wrote in comp.lang.perl.misc:
    > Newbie here. I would like to search through a text file and extract a
    > word from a sentence. For example,
    >
    > " I have 10 apples"
    > " I have 20 apples"
    >
    > How can I extract the 10 and 20 from the sentence? The rest of the
    > words in the sentence is always fixed.


    my ( $extract) = /^I have (.*) apples$/;

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Feb 6, 2006
    #2
    1. Advertising

  3. "silverfish" <> wrote in news:1139214894.747493.230230
    @o13g2000cwo.googlegroups.com:

    > Newbie here. I would like to search through a text file and extract a
    > word from a sentence. For example,
    >
    > " I have 10 apples"
    > " I have 20 apples"
    >
    > How can I extract the 10 and 20 from the sentence? The rest of the
    > words in the sentence is always fixed.


    What have you tried so far?

    Have you read the posting guidelines for this group?

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Feb 6, 2006
    #3
  4. -berlin.de (Anno Siegel) wrote in news:ds721m$9ha$1
    @mamenchi.zrz.TU-Berlin.DE:

    > silverfish <> wrote in comp.lang.perl.misc:
    >> Newbie here. I would like to search through a text file and extract a
    >> word from a sentence. For example,
    >>
    >> " I have 10 apples"
    >> " I have 20 apples"
    >>
    >> How can I extract the 10 and 20 from the sentence? The rest of the
    >> words in the sentence is always fixed.

    >
    > my ( $extract) = /^I have (.*) apples$/;


    According to the OP, the first character is a space.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Feb 6, 2006
    #4
  5. silverfish

    Anno Siegel Guest

    A. Sinan Unur <> wrote in comp.lang.perl.misc:
    > -berlin.de (Anno Siegel) wrote in news:ds721m$9ha$1
    > @mamenchi.zrz.TU-Berlin.DE:
    >
    > > silverfish <> wrote in comp.lang.perl.misc:
    > >> Newbie here. I would like to search through a text file and extract a
    > >> word from a sentence. For example,
    > >>
    > >> " I have 10 apples"
    > >> " I have 20 apples"
    > >>
    > >> How can I extract the 10 and 20 from the sentence? The rest of the
    > >> words in the sentence is always fixed.

    > >
    > > my ( $extract) = /^I have (.*) apples$/;

    >
    > According to the OP, the first character is a space.


    Oh yes, right.

    my ( $extract) = /^ I have (.*) apples$/;

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Feb 6, 2006
    #5
  6. silverfish

    Xicheng Guest

    silverfish wrote:
    > Newbie here. I would like to search through a text file and extract a
    > word from a sentence. For example,
    >
    > " I have 10 apples"
    > " I have 20 apples"
    >
    > How can I extract the 10 and 20 from the sentence? The rest of the
    > words in the sentence is always fixed.


    If you want to grab all these numbers, use an array, say:

    my @extracts = m{" I have (\d+) apples"}g;
    print "@extracts\n";

    to extract all such numbers in your file:
    perl -ln0777e '@e=/" I have (\d+) apples"/g;print "@e"' mydata

    Xicheng


    >
    > thanks for any help!
     
    Xicheng, Feb 6, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GIMME
    Replies:
    2
    Views:
    893
    GIMME
    Feb 11, 2004
  2. .Net Sports
    Replies:
    11
    Views:
    1,454
    .Net Sports
    Jan 17, 2006
  3. Naren
    Replies:
    0
    Views:
    591
    Naren
    May 11, 2004
  4. Kai Schlamp
    Replies:
    1
    Views:
    423
    Arne Vajhøj
    Mar 27, 2008
  5. Domenico Discepola

    Assistance parsing text file using Text::CSV_XS

    Domenico Discepola, Sep 1, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    457
    Domenico Discepola
    Sep 2, 2004
Loading...

Share This Page