Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Extracting numbers from a text file

by PetreAdi (Acolyte)
on Nov 02, 2019 at 17:45 UTC ( [id://11108250]=perlquestion: print w/replies, xml ) Need Help??

PetreAdi has asked for the wisdom of the Perl Monks concerning the following question:

I have a file that looks like

aaa111bbb ab01ac d ab02ac ab03ac aaa222bbb ab04acab05ac r ab06ac aaa333bbb ab01ac t cab06acab10ac

I need to extract the number between 'aaa' and 'bbb' and then pick the following line and extract all numbers between 'ab' and 'ac'

Output would be:

111 01,02,03 222 04,05,06 333 01,06,10

My basic regex is:

use strict; use warnings; my $start = 'aaa'; my $end = 'bbb'; my $start1 = 'ab'; my $end1 = 'ac'; my @matches = $content =~ /${start}([0-9]{3})${end}.*?${start1}([0-9] +{2})${end1}/smg;

Replies are listed 'Best First'.
Re: Extracting numbers from a text file
by haukex (Archbishop) on Nov 02, 2019 at 18:36 UTC

    This produces the desired output for the input you showed:

    perl -ple '$_=join",",/\d+/g' input.txt

    But seriously, it seems like you're not telling us the whole story. Not only does this sound like homework, but your example input & output or specification don't indicate:

    • whether there can be lines without any of the target characters, and if yes, where they can appear,
    • whether there can be any other characters other than the ones you showed on each line, and if yes, where they can appear,
    • what happens if the things between the target characters aren't numbers,
    • why you said "extract all numbers" but your existing regex appears to only want a specific number of digits.
    • There's probably a couple more questions that I'm not thinking of at the moment.
    • Update: Are there always three numbers on the line following the single line?

    Technically, my example above solves the problem you posed, so if you want a regex that really works for you, you'll have to be a lot more specific.

      I'm only getting the first occurrence in {start1}(0-9{2})${end1} (I want to store all matches in the next line)

Re: Extracting numbers from a text file
by tybalt89 (Monsignor) on Nov 02, 2019 at 20:43 UTC
    #!/usr/bin/perl use strict; use warnings; my $content = <<''; aaa111bbb ab01ac d ab02ac ab03ac aaa222bbb ab04acab05ac r ab06ac aaa333bbb ab01ac t cab06acab10ac print "$1\n$2,$3,$4\n" while $content =~ /aaa(\d+)bbb\nab(\d+)ac.*?ab(\d+)ac.*?ab(\d+)ac/g;

      Thank you!

Re: Extracting numbers from a text file
by thanos1983 (Parson) on Nov 02, 2019 at 18:36 UTC

    Hello PetreAdi,

    Something like that?

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @array = ("aaa111bbb", "ab01ac d ab02ac ab03ac", "aaa222bbb", "ab04 +acab05ac r ab06ac", "aaa333bbb", "ab01ac t cab06acab10ac"); foreach my $str (@array) { my @all_nums = $str =~ /(\d+)/g; print Dumper \@all_nums; } __END__ $ perl test.pl $VAR1 = [ '111' ]; $VAR1 = [ '01', '02', '03' ]; $VAR1 = [ '222' ]; $VAR1 = [ '04', '05', '06' ]; $VAR1 = [ '333' ]; $VAR1 = [ '01', '06', '10' ];

    On the Monastery it has been answered before this question (extracting number from string).

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      I have a variable my $content = get($url) (not array)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11108250]
Approved by beech
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (None)
    As of 2024-04-25 00:12 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found