Parsing a text file

S

silverfish

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!
 
A

Anno Siegel

silverfish said:
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
 
A

A. Sinan Unur

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

A. Sinan Unur

(e-mail address removed)-berlin.de (Anno Siegel) wrote in @mamenchi.zrz.TU-Berlin.DE:
my ( $extract) = /^I have (.*) apples$/;

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

Sinan
 
A

Anno Siegel

A. Sinan Unur said:
(e-mail address removed)-berlin.de (Anno Siegel) wrote in @mamenchi.zrz.TU-Berlin.DE:


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

Oh yes, right.

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

Anno
 
X

Xicheng

silverfish said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top