Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Add newline to regex

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-29-2012
Boomn4x4 Boomn4x4 is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 17 January 2013, 8:21 AM EST
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Add newline to regex

I have a perl script that runs a program and puts the output of the program into a variable. The output of the file looks like:

Code:
Statistics 0
    AverageTime: [0,0]
Statistics 0
    AverageTime: [124,0]

I'm trying to run a regular express against this to pull out the first value in each of the parens and put it into an array

The expression I have is:

Code:
$result = `$program`;
@matches = ( $result =~ /AverageTime: \[(.*).\,/g);

When I print the arrary:
print "@matches\n";

I get all of the requested data, but its all on one line separated with spaces.

Code:
0 124

How do I get each match to be its on own in the array?

Last edited by Scott; 11-29-2012 at 12:25 PM.. Reason: Code tags, please...
Sponsored Links
    #2  
Old Unix and Linux 11-29-2012
birei birei is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 4 July 2015, 1:29 PM EDT
Location: Bilbao, Spain
Posts: 628
Thanks: 8
Thanked 173 Times in 171 Posts
Hi Boomn4x4,

You were near. Here a code that works:

Code:
$ cat script.pl
#!/usr/bin/env perl

use warnings;
use strict;
use Data::Dumper;

my $result = <<'EOF';
Statistics 0
    AverageTime: [0,0]
Statistics 0
    AverageTime: [124,0]
EOF

my @matches = ( $result =~ m/\[(\d+),\d+\]/g );

print Dumper \@matches;
$ perl script.pl
$VAR1 = [
          '0',
          '124'
        ];

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Add a newline after every period danbroz Shell Programming and Scripting 4 11-20-2012 04:05 AM
How can I add a newline In a text file using Shell Script kpopfreakghecky Shell Programming and Scripting 1 08-29-2011 11:12 PM
RegEx in Java. \b does not react to a newline machinogodzilla Programming 1 05-14-2010 12:35 AM
add newline in file after finding specific text jxh461 Shell Programming and Scripting 4 07-23-2009 12:31 AM
grep and sed to find a pattern and add newline ssikhar Shell Programming and Scripting 5 10-26-2004 05:32 PM



All times are GMT -4. The time now is 09:31 PM.