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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PERL - Using regular expression to manipulate text

Status
Not open for further replies.

chris01010

Programmer
Jan 29, 2004
25
GB
Hi,

I have a PERL script which executes a script which interrogates a database on another server and produces an output file (an example of this is at the bottom). Of this file I only want a section of the data, from USER until the last number. I have written the code below but when run it doesn't return anything.

Any ideas?

Code:

Code:
undef $/;

$_ = <>;

$var = /^USER.*/;

print "$var";


Output:

Pseudo-terminal will not be allocated because stdin is not a terminal.

mesg: cannot stat

SQL*Plus: Release 11.2.0.2.0 Production on Tue Nov 22 16:40:51 2011

Copyright (c) 1982, 2010, Oracle. All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> SQL> SQL> SQL> 2 3 4 5 6 7
USER OPERATOR CURRENTSESSIONID LASTOUTBOUNDSEQ LASTOUTBOUNDACKED
----------- -------- ---------------- --------------- -----------------
AAAAAAAA OP1 0 27427 22776
BBBBBBBB OP2 0 556006 555361
CCCCCCCC OP3 0 13291 10955
DDDDDDDD OP4 202200 16354418 16353714
EEEEEEEE OP5 0 1685781 1550961
FFFFFFFF OP6 0 3 0
GGGGGGGG OP7 202182 2692535 2692507
SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options


I'm new to PERL so any help would be greatly appreciated.
 
Hi

chris01010 said:
from USER until the last number.
Is suppose you not really mean the last number, as that would be 64.

This puts into $var the part up to the last digit found at the end of a line :
Perl:
[teal]([/teal][navy]$var[/navy][teal])[/teal] [teal]=[/teal] [b]m[/b][fuchsia]/^(USER.*\d)$/[/fuchsia]sm[teal];[/teal]


Feherke.
 
Thanks feherke,

That works! And yes you were right I did mean until 2692507 and not 64.

If you have a moment though could you please explain to me why your syntax resulted in it to end at 2692507? Is it because even though I am "slurping" it still reads it line by line (as opposed to one block) and prints any that fit the criteria?

Thanks again,

Chris
 
Hi

Chris said:
though I am "slurping" it still reads it line by line (as opposed to one block)
Your expression said [tt]^USER#, which means string "USER" at the beginning of string". But the "USER" is not at the beginning of the string, but in the middle of the string, after an embedded newline. To consider the embedded newlines as line separators, I added the [/tt]m## ( multiline ) modifier :
Code:
[blue]  DB<1>[/blue] $s = "One\nTwo\nThree" 

[blue]  DB<2>[/blue] print $s =~ m/^O/
1
[blue]  DB<3>[/blue] print $s =~ m/^T/

[blue]  DB<4>[/blue] print $s =~ m/\nT/
1
[blue]  DB<5>[/blue] print $s =~ m/^T/m
1
See the Modifiers section in man perlre for explanation. Also on why I added the [tt]s[/tt] ( single line ) flag too.


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top