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

regex extracting string 1

Status
Not open for further replies.

dietmarp

Programmer
Dec 27, 2001
53
US
Hi again,
got another problem I didn't solve.
I need to extract the text wrapped by the ".

$line="msg PRINTF("syslogd: %s: No space left on device", logfile)";

Result should read:

$linenew="syslogd: %s: No space left on device"

How can I do it? Are there different ways?

Dietmar
 
A regular expression should do the job:
[tt]
$line=~/"(.*)"/;
$linenew=$1;
[/tt]
 
or:

my $line = "msg PRINTF(\"syslogd: %s: No space left on device\", logfile)";
my ($new) = $line =~ /"([^"]+)"/;

which makes the match non greedy in case there are more than one set of double-quotes.
 
Hi Kevin,
your regex works, if you put the backslashes in front of the quotes.

#!/usr/bin/perl
my $line = "msg PRINTF(\"syslogd: %s: No space left on device\", logfile)";
my ($new) = $line =~ /"([^"]+)"/;
print $new;

syslogd: %s: No space left on device

But without the quotes (as my original line to match) I receive following error:

Bareword found where operator expected at a.pl line 2, near ""msg PRINTF("syslogd"
(Missing operator before syslogd?)
String found where operator expected at a.pl line 2, near "device", logfile)""
syntax error at a.pl line 2, near ""msg PRINTF("syslogd"
Execution of a.pl aborted due to compilation errors.

Any idea why? Cheers - Dietmar


 
I had to put the back-slashes in because your code is not valid perl syntax, which is why you are getting the error. You can't put double-quotes inside of a double-quoted string unless the are escaped with a back-slash. Put single quotes around your string and it will work.
 
The line I'm matching is not perl code. It's from a file which is used by a logfile parsing tool (from IBM Tivoli) containing defintions. (which logfile entries should be found and what messages should be send to a central console - as info for the system admins).
For documentation purpose I want to grep certain infos out of this file and put them into a csv file to import into excel. That's why I can't put back-slashes into it, because the ITM Tivoli tool wouldn't run.
 
if you are reading in the lines from a file that should be OK since perl will treat those as single-quoted strings. Bareword errors are often because of missing quotes or other type of syntax error in a perl script. Like:

my $foo = bar;

bar is considered a bareword. If you code isn't too long maybe you can post it and someone can have a look. Some sample lines of the data might be helpful too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top