For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > removing single quotation marks from a string ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author removing single quotation marks from a string ?
Gregory Machin

2006-11-16, 7:56 am

Hi
I need to remove all the quotation marks from, a string
I tried s/'// but it did not work.
what have i missed ??

Many Thanks

--
Gregory Machin
gregory.machin@gmail.com
www.linuxpro.co.za
John W. Krahn

2006-11-16, 7:56 am

Gregory Machin wrote:
> Hi


Hello,

> I need to remove all the quotation marks from, a string
> I tried s/'// but it did not work.
> what have i missed ??


You need to use the /g (global) option:

s/'//g

Or better yet, use the tr/// operator:

tr/'//d



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
Gregory Machin

2006-11-17, 3:56 am

Hi
I'm still not getting the result i require please could someone have a
quick look and see what i'm missing ...

the following code is suposed to break up the looked info from dns
server so I can manipulate it.
It dies with the following errors when it reads more than one line
from the database ....

Can't call method "fetchrow" without a package or object reference at
../test.pl line 13.
<code>
#!/usr/bin/perl

#$test = "15-Nov-2006 14:11:06.304 update: info: client
196.211.85.42#35435: view external: updating zone
'goal.lek.linserv.co.za/IN': add
use Mysql;
$dbh = Mysql-> connect('localhost','syslog_ng','root','
')
or die("Error " . $dbh->errno . " - " . $dbh->errstr);
$query = $dbh->query("SELECT msg FROM logs where program = 'named'; ")
or die("Error " . $dbh->errno . " - " . $dbh->errstr);
while (@row = $query->fetchrow){
#chomp ;
#if ($test =~ /^(\d+)-(\w+)-(\d+) (\d+):(\d+):(\d+).*?client
(\d+)(.\d+)(.\d+)(.\d+)#(\d+): view external: (.*)$/){
$_ =~ tr/'//d ;
if ($row[msg] =~ /^(\d+)-(\w+)-(\d+) (\d+):(\d+):(\d+).*?client
(\d+)(.\d+)(.\d+)(.\d+)#(\d+): view external: updating zone (.*)$/){
$day = $1;
$month = $2;
$year = $3;
$hour = $4;
$minute = $5;
$second = $6;
$clientip = $7;
$clientip .= $8;
$clientip .= $9;
$clientip .= $10;
$port = $11;
$query = $12;

print("day is $day\n");
print("month is $month\n");
print("year is $year\n");
print("hour is $hour\n");
print("minute is $minute\n");
print("second is $second\n");
print("clientip is $clientip\n");
print("port is $port\n");
print("query is $query\n");
print("\n");

}else{
print("malformed log entry: $_ \n");
}

</code>

$test is a sample of the syntex.

On 11/16/06, Gregory Machin <gregory.machin@gmail.com> wrote:
> Hi
> I need to remove all the quotation marks from, a string
> I tried s/'// but it did not work.
> what have i missed ??
>
> Many Thanks
>
> --
> Gregory Machin
> gregory.machin@gmail.com
> www.linuxpro.co.za
>



--
Gregory Machin
gregory.machin@gmail.com
www.linuxpro.co.za
Mumia W.

2006-11-17, 9:57 pm

On 11/17/2006 01:22 AM, Gregory Machin wrote:
>
>
> Hi


Hi Gregory Machin. You top-posted. Please don't do that. I corrected the
top-posing and made it a bottom-post.

> I'm still not getting the result i require please could someone have a
> quick look and see what i'm missing ...
>
> the following code is suposed to break up the looked info from dns
> server so I can manipulate it.
> It dies with the following errors when it reads more than one line
> from the database ....
>
> Can't call method "fetchrow" without a package or object reference at
> ./test.pl line 13.
> <code>
> #!/usr/bin/perl


use strict;
use warnings;
# Change your program to work with these.

>
> #$test = "15-Nov-2006 14:11:06.304 update: info: client
> 196.211.85.42#35435: view external: updating zone
> 'goal.lek.linserv.co.za/IN': add
> use Mysql;
> $dbh = Mysql-> connect('localhost','syslog_ng','root','
')
> or die("Error " . $dbh->errno . " - " . $dbh->errstr);


Why would you invoke errno and errstr on an undefined value? $dbh has to
be undefined if the die clause is being executed.

> $query = $dbh->query("SELECT msg FROM logs where program = 'named'; ")
> or die("Error " . $dbh->errno . " - " . $dbh->errstr);
> while (@row = $query->fetchrow){


You use $query to get access to the query data--okay.

> #chomp ;
> #if ($test =~ /^(\d+)-(\w+)-(\d+) (\d+):(\d+):(\d+).*?client
> (\d+)(.\d+)(.\d+)(.\d+)#(\d+): view external: (.*)$/){
> $_ =~ tr/'//d ;
> if ($row[msg] =~ /^(\d+)-(\w+)-(\d+) (\d+):(\d+):(\d+).*?client
> (\d+)(.\d+)(.\d+)(.\d+)#(\d+): view external: updating zone (.*)$/){


What is "msg"? Shouldn't you be trying to access "$row[0]" ?

> $day = $1;
> $month = $2;
> $year = $3;
> $hour = $4;
> $minute = $5;
> $second = $6;
> $clientip = $7;
> $clientip .= $8;
> $clientip .= $9;
> $clientip .= $10;
> $port = $11;
> $query = $12;
>


You just clobbered your query variable. It was set to the return value
of "$dbh->query(...)" earlier, but now it gets replaced with some text,
and that makes it impossible to get any more information out of that query.

This is the reason for your error.


> print("day is $day\n");
> print("month is $month\n");
> print("year is $year\n");
> print("hour is $hour\n");
> print("minute is $minute\n");
> print("second is $second\n");
> print("clientip is $clientip\n");
> print("port is $port\n");
> print("query is $query\n");
> print("\n");
>
> }else{
> print("malformed log entry: $_ \n");
> }
>
> </code>
>
> $test is a sample of the syntex.
>


I think you mean "syntax."

Please don't top-post, or I won't read any more of your messages. And
please try to use correct English spelling and grammar.

This works:

use Mysql;
use strict;
use warnings;

my $regex = '^(\d+)-(\w+)-(\d+) (\d+):(\d+):(\d+).*?client'
.. '\s(\d+)(.\d+)(.\d+)(.\d+)#(\d+): view external: (.*)$';

my $dbh = Mysql->connect('localhost','test','')
or die("Connect failed.");
my $search = $dbh->query('select msg from logfiles')
or die("Query failed: " . $dbh->errstr);

while (my @row = $search->fetchrow) {
no strict 'vars';
$row[0] =~ tr/'//d;
if ($row[0] =~ /$regex/os) {
$day = $1;
$month = $2;
$year = $3;
$hour = $4;
$minute = $5;
$second = $6;
$clientip = $7;
$clientip .= $8;
$clientip .= $9;
$clientip .= $10;
$port = $11;
$query = $12;

print("day is $day\n");
print("month is $month\n");
print("year is $year\n");
print("hour is $hour\n");
print("minute is $minute\n");
print("second is $second\n");
print("clientip is $clientip\n");
print("port is $port\n");
print("query is $query\n");
print("\n");
} else {
print "Malformed Log Entry: $row[0]\n";
}
}

undef $search;
undef $dbh;


John W. Krahn

2006-11-17, 9:57 pm

Mumia W. wrote:
>
> This works:
>
> use Mysql;
> use strict;
> use warnings;
>
> my $regex = '^(\d+)-(\w+)-(\d+) (\d+):(\d+):(\d+).*?client'
> . '\s(\d+)(.\d+)(.\d+)(.\d+)#(\d+): view external: (.*)$';


You need to escape the . character to match a literal period. You should
probably also use the qr// operator:

my $regex = qr/
^
(\d+)-(\w+)-(\d+)
\s+
(\d+):(\d+):(\d+)
.*?client\s+
(\d+\.\d+\.\d+\.\d+)\#(\d+)
:\s+view external:\s+
(.*)
$
/x;


> my $dbh = Mysql->connect('localhost','test','')
> or die("Connect failed.");
> my $search = $dbh->query('select msg from logfiles')
> or die("Query failed: " . $dbh->errstr);
>
> while (my @row = $search->fetchrow) {
> no strict 'vars';


Why are you turning off strict?

> $row[0] =~ tr/'//d;
> if ($row[0] =~ /$regex/os) {
> $day = $1;
> $month = $2;
> $year = $3;
> $hour = $4;
> $minute = $5;
> $second = $6;
> $clientip = $7;
> $clientip .= $8;
> $clientip .= $9;
> $clientip .= $10;
> $port = $11;
> $query = $12;
>
> print("day is $day\n");
> print("month is $month\n");
> print("year is $year\n");
> print("hour is $hour\n");
> print("minute is $minute\n");
> print("second is $second\n");
> print("clientip is $clientip\n");
> print("port is $port\n");
> print("query is $query\n");
> print("\n");


Why all the copying?

if ( $row[ 0 ] =~ /$regex/ ) {
print <<TEXT;
day is $1
month is $2
year is $3
hour is $4
minute is $5
second is $6
clientip is $7
port is $8
query is $9

TEXT

> } else {
> print "Malformed Log Entry: $row[0]\n";
> }
> }
>
> undef $search;
> undef $dbh;




John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
Gregory Machin

2006-11-23, 6:57 pm

Thanks for all the advice :-).
I'll follow up on it shortly, this is just my dummy script for testing :-)
Have a grate day ..

On 11/17/06, John W. Krahn <krahnj@telus.net> wrote:
> Mumia W. wrote:
>
> You need to escape the . character to match a literal period. You should
> probably also use the qr// operator:
>
> my $regex = qr/
> ^
> (\d+)-(\w+)-(\d+)
> \s+
> (\d+):(\d+):(\d+)
> .*?client\s+
> (\d+\.\d+\.\d+\.\d+)\#(\d+)
> :\s+view external:\s+
> (.*)
> $
> /x;
>
>
>
> Why are you turning off strict?
>
>
> Why all the copying?
>
> if ( $row[ 0 ] =~ /$regex/ ) {
> print <<TEXT;
> day is $1
> month is $2
> year is $3
> hour is $4
> minute is $5
> second is $6
> clientip is $7
> port is $8
> query is $9
>
> TEXT
>
>
>
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you can special-order
> certain sorts of tools at low cost and in short order. -- Larry Wall
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>



--
Gregory Machin
gregory.machin@gmail.com
www.linuxpro.co.za
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com