Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > perl parse across multiple line in txt file

Reply
Thread Tools

perl parse across multiple line in txt file

 
 
bcdixit
Guest
Posts: n/a
 
      08-19-2007
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

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      08-19-2007
bcdixit <(E-Mail Removed)> 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/"
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      08-19-2007
bcdixit <(E-Mail Removed)> 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/"
 
Reply With Quote
 
bcdixit
Guest
Posts: n/a
 
      08-19-2007
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 <(E-Mail Removed)> wrote:
> bcdixit <(E-Mail Removed)> 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/"



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
counting how often the same word appears in a txt file...But my codeonly prints the last line entry in the txt file dgcosgrave@gmail.com Python 8 12-19-2012 06:29 PM
How to read .txt file line by line jobo Javascript 1 04-17-2007 10:42 AM
Best way to parse a small txt file arak123@email.com C++ 4 10-04-2005 03:46 PM
Opening a txt file to view ( i.e. readme.txt) Sameen C++ 2 08-29-2005 03:14 PM
How to parse a string like C program parse the command line string? linzhenhua1205@163.com C Programming 19 03-15-2005 07:41 PM



Advertisments