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!

Carriage Returns are Killing my Script - Assistance Required 1

Status
Not open for further replies.

CTSQLGUY

Technical User
Feb 4, 2007
33
US
I’m running into issues because of carriage returns and need some assistance…

I’m using this to capture the description:

-------------------------------------------------------------------------------------
my $description = '';
if ($record =~ /^\s+DESCRIPTION\n\s+"(.*?)"$/m) {
$description = $1;
}
-------------------------------------------------------------------------------------

That works fine as long as the description is a single line – it captures nothing if it goes to a new one.

It captures this description fine:

-------------------------------------------------------------------------------------
topology OBJECT IDENTIFIER ::= { directorFamily 1 }

online NOTIFICATION-TYPE
OBJECTS { trapType, trapSeverity, trapSenderName, trapManagedObjectName, trapText, trapCategory }
STATUS current
DESCRIPTION
"The managed object's state has changed to online."
::= { topology 1 }
-------------------------------------------------------------------------------------



This one dies:

-------------------------------------------------------------------------------------
processMonitor NOTIFICATION-TYPE
OBJECTS { trapType, trapSeverity, trapSenderName, trapManagedObjectName, trapText, trapCategory,
actualValue, duration, monitorResource, thresholdName, thresholdValue }
STATUS current
DESCRIPTION
"The process monitor threshold has generated
an event."
::= { directorAgent 1 }
-------------------------------------------------------------------------------------

Any ideas? All I'm trying to do is capture the text in between the quotes...

Thanks for the help!
 
Code:
my $record = q{processMonitor NOTIFICATION-TYPE
   OBJECTS { trapType, trapSeverity, trapSenderName, trapManagedObjectName, trapText, trapCategory,
             actualValue, duration, monitorResource, thresholdName, thresholdValue }
   STATUS  current
   DESCRIPTION
      "The process monitor threshold has generated
      an event."
::= { directorAgent 1 }};

my $description = '';
if ($record =~ /\s+DESCRIPTION\n\s+"(.*?)"/s) {
    $description = $1;
}
print $description;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks, but the text string will change - sorry if I was to general but I have multiple records I'm going to be running this through and all I need to focus on is the text between the quotes...

Thanks though!
 
Should work even if there is some difference than your sample data. Try it.

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

That's *very close* - the one thing I need to happen though is for the message to end up on a single line... I'm not sure how to make that happen.

*shrug*

This is my output:

-------------------------------------------------
message = The process count monitor threshold has generated
an event.
-------------------------------------------------

I need all of the messages to end up on a single line...

Thanks again!
 
simply use the s/// operator to remove the characters from $description
 
I usually remove newline characters by their hex codes if it's really important that both chars get removed:

s/[\x0d\x0a]//g;

The reason is because in Perl, \r is a carriage return, but \n is NOT necessarily a line feed. Instead, \n actually is the native EOL characters on the host operating system, so when running under Windows, \n is actually "\r\n", or CRLF, but on Linux it's actually LF like you'd expect.

Thus printing "\r\n" on Windows doesn't do exactly what you'd think, as it literally prints "\r\r\n" and if you give a sequence like that to i.e. a server that's really picky about those characters, the server might kick you off or something.

So when \r\n, \n, and \r are important to deal with, I use their hex counterparts because that way I know that \x0d is a CR and \x0a is a LF regardless of the OS.

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top