perl parse across multiple line in txt file

Discussion in 'Perl Misc' started by bcdixit, Aug 19, 2007.

  1. bcdixit

    bcdixit Guest

    i have a file with the following sample text

    1 create table xyz
    2 no before journal,
    3 no after journal
    4 (
    5 col1 integer,
    6 col2 integer,
    7 ...
    8 coln varchar(10)
    9 )
    10;
    i want to use perl regex to search and replace text from the line that
    starts with 'create' word till the first opening bracket i.e the '('
    with blanks or rather delete the lines altogether.
    the output should look something like this.


    1 col1 integer,
    2 col2 integer,
    3 ...
    4 coln varchar(10)
    5 )
    6;

    the input file could also have the following scenarios..
    1 create table xyz no before journal,
    2 no after journal
    3 (
    4 col1 integer,
    5 col2 integer,
    6 ...
    7 coln varchar(10)
    8 )
    9;

    OR


    1 create table xyz no before journal,
    2 no after journal (
    3 col1 integer,
    4 col2 integer,
    5 ...
    6 coln varchar(10)
    7 )
    8;

    OR

    1 create table xyz
    2 (
    3 col1 integer,
    4 col2 integer,
    5 ...
    6 coln varchar(10)
    7 )
    8;

    the only certainty is that line starts with the 'create' word. THERE
    COULD BE ANY WORDS BETWEEN THE 'CREATE' AND THE '(' .
    so in short, i want the search to look for any line that begins with
    the 'create' word and then continue the search till the first '(' and
    replace the match with deleted lines.

    I know how to use perl regex to search for one line at a time but not
    if the condition could be across multiple lines.

    any help will be greatly appreciated.

    thanks
    bcdixit, Aug 19, 2007
    #1
    1. Advertising

  2. bcdixit <> wrote:
    > i have a file with the following sample text
    >
    > 1 create table xyz
    > 2 no before journal,
    > 3 no after journal
    > 4 (
    > 5 col1 integer,
    > 6 col2 integer,
    > 7 ...
    > 8 coln varchar(10)
    > 9 )
    > 10;
    > i want to use perl regex to search and replace text from the line that
    > starts with 'create' word till the first opening bracket i.e the '('
    > with blanks or rather delete the lines altogether.
    > the output should look something like this.
    >
    >
    > 1 col1 integer,
    > 2 col2 integer,
    > 3 ...
    > 4 coln varchar(10)
    > 5 )
    > 6;



    perldoc -q between

    How can I pull out lines between two patterns that are themselves on difâ€
    ferent lines?


    --------------------------
    #!/usr/bin/perl
    use warnings;
    use strict;

    my $cnt=1;
    while ( <DATA> ) {
    next if /^\d+ create/ .. /^\d+ \(/;
    s/^\d+/$cnt/;
    $cnt++;
    print;
    }

    __DATA__
    1 create table xyz
    2 no before journal,
    3 no after journal
    4 (
    5 col1 integer,
    6 col2 integer,
    7 ...
    8 coln varchar(10)
    9 )
    10;
    --------------------------


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad McClellan, Aug 19, 2007
    #2
    1. Advertising

  3. bcdixit <> wrote:
    > i have a file with the following sample text
    >
    > 1 create table xyz
    > 2 no before journal,
    > 3 no after journal
    > 4 (
    > 5 col1 integer,
    > 6 col2 integer,
    > 7 ...
    > 8 coln varchar(10)
    > 9 )
    > 10;
    > i want to use perl regex to search and replace text from the line that
    > starts with 'create'



    There is no line there that starts with 'create'.

    There is a line that starts with '1', and with '2', and ...


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad McClellan, Aug 19, 2007
    #3
  4. bcdixit

    bcdixit Guest

    sorry if I was not too clear. Those numbers are actually line numbers.
    those numbers are not actually part of the text.

    On Aug 18, 9:13 pm, Tad McClellan <> wrote:
    > bcdixit <> wrote:
    > > i have a file with the following sample text

    >
    > > 1 create table xyz
    > > 2 no before journal,
    > > 3 no after journal
    > > 4 (
    > > 5 col1 integer,
    > > 6 col2 integer,
    > > 7 ...
    > > 8 coln varchar(10)
    > > 9 )
    > > 10;
    > > i want to use perl regex to search and replace text from the line that
    > > starts with 'create'

    >
    > There is no line there that starts with 'create'.
    >
    > There is a line that starts with '1', and with '2', and ...
    >
    > --
    > Tad McClellan
    > email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    bcdixit, Aug 19, 2007
    #4
    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. Replies:
    19
    Views:
    1,121
    Daniel Vallstrom
    Mar 15, 2005
  2. Sameen
    Replies:
    2
    Views:
    440
    Victor Bazarov
    Aug 29, 2005
  3. Replies:
    4
    Views:
    332
  4. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,541
    Roedy Green
    Sep 15, 2011
  5. jobo

    How to read .txt file line by line

    jobo, Apr 17, 2007, in forum: Javascript
    Replies:
    1
    Views:
    87
    scripts.contact
    Apr 17, 2007
Loading...

Share This Page