K
Kevin Walzer
I am parsing a text-delimited file that has been saved as a single line.
It's actually supposed to be a multiline file with a date in yyyy-mm-dd
format as the first field on each line. To correctly structure the data,
I'm trying to insert insert a newline character before all date fields
and and then split the file into an array on the newline char. The issue
I'm running into is that I can't get the substitution pattern right.
Here's my code:
my $date1="([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])";
my $date2="\n([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])";
$_ =~ s/$date1/$date2/g;
This pattern appears to correctly match the date field, but rather
simply inserting a newline in front of the date field's data it inserts
a newline and then a string literal that looks like this:
([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])
What I want is to replace 2012-01-01 with \n2012-01-01, and all
subsequent date fields in the file in the same fashion. Can someone
suggest a better way to do this?
Thanks,
Kevin
It's actually supposed to be a multiline file with a date in yyyy-mm-dd
format as the first field on each line. To correctly structure the data,
I'm trying to insert insert a newline character before all date fields
and and then split the file into an array on the newline char. The issue
I'm running into is that I can't get the substitution pattern right.
Here's my code:
my $date1="([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])";
my $date2="\n([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])";
$_ =~ s/$date1/$date2/g;
This pattern appears to correctly match the date field, but rather
simply inserting a newline in front of the date field's data it inserts
a newline and then a string literal that looks like this:
([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])
What I want is to replace 2012-01-01 with \n2012-01-01, and all
subsequent date fields in the file in the same fashion. Can someone
suggest a better way to do this?
Thanks,
Kevin