David Gilden wrote:
>
> I am having a little trouble understanding matching and getting the sub 
pattern
> saved to a Var. so that I can do a munge. I want to take the line returns and change them into
> pipe characters '|'
> All data records start with a date i.e. 01/01/2006
> But there are fields in between that are one multiple lines. Sample data is below the script.
>
> Here's what I have so far....
>
> #!/usr/bin/perl -w
>
>
> open(FI,"test.txt") || die "Read in, Could not find File, $!";
> my @files = <FI>;
> close(FI);
>
> for (@files){
>
> chomp($_);
>
> $_ =~m|(\d+/\d+/\d+)(?s)(.+?)\d+/\d+/\d+|;
>
> $2 =~tr/\n/|/;
>
> print "$1$2\n";
>
> # save out modified result...
> }
>
> Given this data:
>
> 09/01/2006|03:29AM Password for qsocordovam reset by Self Reset utility
> Username: qsocordovam
> ClientNumber: 77927
> IP address: 24.248.1.241
> 09/01/2006|07:53AM Failed reset attempt
> Username: tmpcollic03
> ClientNumber: 110330
> IP address: 152.121.16.7
> Failed challenge question(s)
> 09/01/2006|07:55AM Failed reset attempt
> Username: tmpcollic03
> ClientNumber: 110330
> IP address: 152.121.16.7
> Failed challenge question(s)
> 09/01/2006|08:03AM Failed reset attempt
>
> Desired result:
> 09/01/2006|03:29AM Password for qsocordovam reset by Self Reset utility|Username: qsocordovam|ClientNumber: 77927|IP address: 24.248.1.241
> #Next record.... starts with a date of format : 09/01/2006

The code below will do the trick.

HTH,

Rob


use strict;
use warnings;

my @lines;

while (<DATA>) {
  s/\s*$//;
  if (m#^\d\d/\d\d/\d{4}#) {
    push @lines, $_;
  }
  else {
    $lines[-1] .= "|$_";
  }
}

print "$_\n" foreach @lines;


*OUTPUT*

09/01/2006|03:29AM Password for qsocordovam reset by Self Reset utility|Username: qsocordovam|ClientNumber: 77927|IP address: 24.248.1.241 09/01/2006|07:53AM Failed reset attempt|Username: tmpcollic03|ClientNumber: 110330|IP address: 152.121.16.7|Failed challenge question(s) 09/01/2006|07:55AM Failed reset attempt|Username: tmpcollic03|ClientNumber: 110330|IP address: 152.121.16.7|Failed challenge question(s)
09/01/2006|08:03AM Failed reset attempt

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to