Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl - parse across one or more lines in a text file

Status
Not open for further replies.

bcdixit

Technical User
Nov 11, 2005
64
US
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
 
I think there is a switch (/m or /s) that treats everything as one line.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those Who Say It Cannot Be Done Are Usually Interrupted by Someone Else Doing It; Give the wrong symptoms, get the wrong solutions;
 
I used the 's' (single line) and 'g' (global) modifiers to achieve the result.

Code:
[red]{[/red]
	[url=http://perldoc.perl.org/functions/local.html][black][b]local[/b][/black][/url] [blue]$/[/blue] = [red]"[/red][purple][/purple][red]"[/red][red];[/red]
	[olive][b]while[/b][/olive][red]([/red]<DATA>[red])[/red] [red]{[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$1[/blue] [olive][b]if[/b][/olive] [red]/[/red][purple]create table .*?([purple][b]\([/b][/purple].*?)[purple][b]\)[/b][/purple][purple][b]\n[/b][/purple];[/purple][red]/[/red][red]sg[/red][red];[/red]
	[red]}[/red]
[red]}[/red]

[teal]__DATA__[/teal]
[teal]create table xyz[/teal]
[teal]no before journal,[/teal]
[teal]no after journal[/teal]
[teal]([/teal]
[teal]col1 integer,[/teal]
[teal]col2 integer,[/teal]
[teal]...[/teal]
[teal]coln varchar(10)[/teal]
[teal])[/teal]
[teal];[/teal]

[teal]create table xyz no before journal,[/teal]
[teal]no after journal[/teal]
[teal]([/teal]
[teal]col1 integer,[/teal]
[teal]col2 integer,[/teal]
[teal]...[/teal]
[teal]coln varchar(10)[/teal]
[teal])[/teal]
[teal];[/teal]

[teal]create table xyz no before journal,[/teal]
[teal]no after journal ([/teal]
[teal]col1 integer,[/teal]
[teal]col2 integer,[/teal]
[teal]...[/teal]
[teal]coln varchar(10)[/teal]
[teal])[/teal]
[teal];[/teal]

[teal]create table xyz[/teal]
[teal]([/teal]
[teal]col1 integer,[/teal]
[teal]col2 integer,[/teal]
[teal]...[/teal]
[teal]coln varchar(10)[/teal]
[teal])[/teal]
[teal];[/teal]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top