replace pattern including newline : perl version 5.8

P

Prince Kumar

I have a file with the following contents.

drop table tabl1;

create table tabl1 (
col1
col2
col3);

drop table tabl2;

create table tabl2 (
col1
col2);

...

I want to replace "create table * );" with
"create table * ) IN TBSP1;"

I tried the following, but I am not getting the desired result.

perl -p -e 'undef $/; s/^CREATE TABLE (.*?)\);$/CREATE TABLE $1 \) IN
TBSP/s' my_input_file.

Your help is appreciated.

Thank You!
 
J

John W. Krahn

Prince said:
I have a file with the following contents.

drop table tabl1;

create table tabl1 (
col1
col2
col3);

drop table tabl2;

create table tabl2 (
col1
col2);

..

I want to replace "create table * );" with
"create table * ) IN TBSP1;"

I tried the following, but I am not getting the desired result.

perl -p -e 'undef $/; s/^CREATE TABLE (.*?)\);$/CREATE TABLE $1 \) IN
TBSP/s' my_input_file.


perl -073pe's/^(\s*create\s+table\s+(\S+)\s+\((?s:.+?)\));/$1 IN \U$2;/i'
my_input_file



John
 
P

Prince

Thank you both!

Abigail, that was my mistake. I intended "lowe case" only. Initially I
had the data file in upper case.

Regards,
Prince.
 
P

Prince

Would you also, please explain "((?s:.+?)\))". I tried the perlre, but
couldn't understand completely.

Thanks,
 
A

Arndt Jonasson

The given solution works perfect. What does -073 option do?

Let 'perl' tell you:

% perl -h

Usage: perl [switches] [--] [programfile] [arguments]
-0[octal] specify record separator (\0, if no argument)

So let it tell us what character has the code 073:

% perl -e 'print "(\073)\n"'

(;)
 
J

John W. Krahn

Prince said:
The given solution works perfect. What does -073 option do?

-073 uses the character ';' as the input record separator

Would you also, please explain "((?s:.+?)\))". I tried the perlre, but
couldn't understand completely.

That says match a literal '(' character followed by one or more of any
character including the newline character followed by a literal ')' character.
Normally .+? will not match a newline so you need the /s option to allow it
to match any character. You could also write it like this:

perl -073pe's/^(\s*create\s+table\s+(\S+)\s+\(.+?\));/$1 IN \U$2;/si'
my_input_file



John
 
P

Prince Kumar

Thank you very much!!!

John W. Krahn said:
-073 uses the character ';' as the input record separator



That says match a literal '(' character followed by one or more of any
character including the newline character followed by a literal ')' character.
Normally .+? will not match a newline so you need the /s option to allow it
to match any character. You could also write it like this:

perl -073pe's/^(\s*create\s+table\s+(\S+)\s+\(.+?\));/$1 IN \U$2;/si'
my_input_file



John
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top