Insert newline before date in yyyy-mm-dd format

Discussion in 'Perl Misc' started by Kevin Walzer, Dec 1, 2012.

  1. Kevin Walzer

    Kevin Walzer Guest

    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

    --
    Kevin Walzer
    Code by Kevin
    http://www.codebykevin.com
     
    Kevin Walzer, Dec 1, 2012
    #1
    1. Advertising

  2. Kevin Walzer

    Dave Saville Guest

    On Sat, 1 Dec 2012 18:17:09 UTC, Kevin Walzer <>
    wrote:

    > 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?


    It did what you told it to do :)

    Try s/(\d\d\d\d-\d\d-\d\d)/\n$1/g;
    --
    Regards
    Dave Saville
     
    Dave Saville, Dec 1, 2012
    #2
    1. Advertising

  3. Kevin Walzer

    Kevin Walzer Guest

    On 12/1/12 2:22 PM, Dave Saville wrote:

    >
    > It did what you told it to do :)
    >
    > Try s/(\d\d\d\d-\d\d-\d\d)/\n$1/g;
    >


    Thanks, that did the trick.
    K

    --
    Kevin Walzer
    Code by Kevin
    http://www.codebykevin.com
     
    Kevin Walzer, Dec 2, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q3liZXJMb3R1cw==?=

    Does CompareValidator work for the date in dd-mmm-yyyy format?

    =?Utf-8?B?Q3liZXJMb3R1cw==?=, Jun 10, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    15,896
    Peter Blum
    Jun 10, 2005
  2. Chu
    Replies:
    3
    Views:
    4,220
    Shawn
    Aug 15, 2006
  3. Ashraf Ansari

    convert MM/dd/yyyy format into dd/MM/yyyy

    Ashraf Ansari, Aug 30, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    798
    Alexey Smirnov
    Aug 30, 2007
  4. Gilles Ganault

    Converting DD MM YYYY into YYYY-MM-DD?

    Gilles Ganault, Aug 17, 2009, in forum: Python
    Replies:
    18
    Views:
    1,402
    Mark Tolonen
    Aug 20, 2009
  5. J P Singh
    Replies:
    4
    Views:
    293
    J P Singh
    Feb 10, 2004
Loading...

Share This Page