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!

Assistance capturing multiple lines... 1

Status
Not open for further replies.

CTSQLGUY

Technical User
Feb 4, 2007
33
0
0
US
I have a script that is only able to capture one line at a time - I need it to be able to capture ALL of my data between the quotes.

Here is the script:

-----------------------------------------------------------
my $description = '';
if ($record =~ /^\s+DESCRIPTION\n\s+"(.*?)"$/m) {
$description = $1;
} else {
print OUTFILE "Error: Description not found in record - $record";
}
-----------------------------------------------------------

This is an example of what one of the records look like that I'm grabbing:

-----------------------------------------------------------
"This trap signifies that the SonicWALL appliance is re-initializing itself
such that neither the agent configuration nor the appliance
implementation is altered. "
-----------------------------------------------------------
 
I think you want to change the /m to /s

m
Treat string as multiple lines. That is, change ``^'' and ``$'' from matching at only the very start or end of the string to the start or end of any line anywhere within the string,

s
Treat string as single line. That is, change ``.'' to match any character whatsoever, even a newline, which it normally would not match.



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[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'm sorry... I don't understand.

Right now this is what that line looks like:

---------------------------------------------------------
if ($record =~ /^\s+DESCRIPTION\n\s+"(.*?)"$/m) {
---------------------------------------------------------

So I should change it to the following:

---------------------------------------------------------
if ($record =~ /^\s+DESCRIPTION\n\s+"(.*?)"$/s) {
---------------------------------------------------------

That change alone isn't doing it...

Actually, I just realized... just to complicate the situation more - does anyone know how it would be possible to make those multiple lines become one single line?

So take this example:

---------------------------------------------------------
"This trap signifies that the SonicWALL appliance is re-initializing itself
such that neither the agent configuration nor the appliance
implementation is altered. "
---------------------------------------------------------

And turn it into this:

---------------------------------------------------------
"This trap signifies that the SonicWALL appliance is re-initializing itself such that neither the agent configuration nor the appliance implementation is altered. "
---------------------------------------------------------
 
is $record one line or multiple lines? Are you reading through a file one line at a time or what?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
CTSQLGUY,

Refering back to your original threads:
Jun 7th - Jul 4th -
I would say that all you need is this:

Code:
		[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$description[/blue] = [red]'[/red][purple][/purple][red]'[/red][red];[/red]
		[olive][b]if[/b][/olive] [red]([/red][blue]$record[/blue] =~ [red]/[/red][purple]^[purple][b]\s[/b][/purple]+DESCRIPTION[purple][b]\n[/b][/purple][purple][b]\s[/b][/purple]+"(.*?)"$[/purple][red]/[/red][red]ms[/red][red])[/red] [red]{[/red]
			[blue]$description[/blue] = [blue]$1[/blue][red];[/red]
			[blue]$description[/blue] =~ [red]s/[/red][purple][purple][b]\n[/b][/purple][/purple][red]/[/red][purple] [/purple][red]/[/red][red]g[/red][red];[/red] [gray][i]# Strip any returns.[/i][/gray]
		[red]}[/red] [olive][b]else[/b][/olive] [red]{[/red]
			[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] OUTFILE [red]"[/red][purple]Error: Description not found in record - [blue]$record[/blue][/purple][red]"[/red][red];[/red]
		[red]}[/red]

- Miller
 
Thanks Miller - that was 100% what I needed!

Thanks everyone else for the help!
 
Actually.. I just noticed it isn't removing the returns, it is, but it's leaving gaps in between - this was copied from the output:
--------------------------------------------------------
message = This trap signifies that the SonicWALL appliance is re-initializing itself such that neither the agent configuration nor the appliance implementation is altered.
--------------------------------------------------------

So it's all on one line, but there are giant holes there too - is it easy to remove those? If you point me in the right direction I'm sure I can figure it out.

Thanks!
 
maybe try changing
$description =~ s/\n/ /g;
to
$description =~ s/\n//g;

or adding after the \n line.
$description =~ s/\s+/ /g;
to change all gaps to a single gap

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[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;
 
Code:
[blue]$description[/blue] =~ [red]s/[/red][purple][purple][b]\s[/b][/purple]*[purple][b]\n[/b][/purple][purple][b]\s[/b][/purple]*[/purple][red]/[/red][purple] [/purple][red]/[/red][red]g[/red][red];[/red]

- Miller
 
That did it... this is what I ended up using:

------------------------------------
my $description = '';
if ($record =~ /^\s+DESCRIPTION\n\s+"(.*?)"$/ms) {
$description = $1;
$description =~ s/\n/ /g; # Strip any returns.
$description =~ s/\s+/ /g; # Removes spaces
------------------------------------

Thanks for the help everyone!
 
Oops I didn't see Millers update before my last post, I just tried it and that's 100%!

Thanks again all!
 
Yeah.. Miller's pretty smart [cheers]

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[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;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top