[email protected] (Jim Gibson) writes:
>> On Aug 13, 2017, at 6:02 PM, Harry Putnam <[email protected]> wrote:
>>
>> My aim:
>>
>> Run certain kinds of log file lines thru a perl script that will:
>>
>> 1) Identify each line by regex that finds pattern at start of line
>> 2) When such a line is found, print newline first then
>> 3) wrap any lines longer than specified number of columns.
>>
>>
>> I was not able to divine from `perldoc Text::Wrap' how to really use
>> it to do what I want.
>>
>> my non-working effort stripped to the bare bones:
>>
>> use strict;
>> use warnings;
>> use Text::Wrap;
>>
>> my $rgx = qr/@{[shift]}/;
>>
>> $Text::Wrap::columns = 68;
>>
>> my @text;
>>
>> while (<>) {
>> if (/$rgx/) {
>> print "\n";
>> print wrap(",", @text);
>> }
>> }
>>
>> It wasn't at all clear from perldoc Text::Wrap how @text is supposed
>> to be populated.
>
> @text is a list of scalar strings passed to the wrap subroutine. You
> can pass a single string also. Try this loop instead:
>
> while (<>) {
> if (/$rgx/) {
> print "\n";
> print wrap(",", $_);
> }
> }
>
> It is usually better to use explicit variables:
>
> while ( my $line = <> ) {
> if (/$rgx/) {
> print "\n";
> print wrap(",", $line);
> }
> }
Apparently I do not have the wisdom necessary to make a working
script, even with your patient help.
I'm not sure what to make of the ouput:
With the script in a configuration resembling your first example:
(Note a few lines of sendmail log output in file `logs' posted at the
end)
-------8< snip -------------8< snip --------------
use strict;
use warnings;
use Text::Wrap;
my $rgx = qr/@{[shift]}/;
$Text::Wrap::columns = 68;
while (<>) {
if (/$rgx/) {
print "\n";
print wrap(",", $_);
}
}
-------8< snip -------------8< snip --------------
logwrp '^Aug ' logs
Gets this output:
Increasing $Text::Wrap::columns from 68 to 99 to accommodate length of
subsequent tab at /vcs/d0/home/reader/scripts/perl/logwrp line 35.
Increasing $Text::Wrap::columns from 99 to 146 to accommodate length of
subsequent tab at /vcs/d0/home/reader/scripts/perl/logwrp line 35.
--- END ---
Output included the blank lines as above.
------- ------- ---=--- ------- -------
A second attempt trying to use your last example as inspiration
follows:
-------8< snip -------------8< snip --------------
use strict;
use warnings;
use Text::Wrap;
my $rgx = qr/@{[shift]}/;
$Text::Wrap::columns = 68;
while ( my $line = <> ) {
if (/$rgx/) {
print "\n";
print wrap(",", $line);
}
}
-------8< snip -------------8< snip --------------
Output from same `logs' file:
Use of uninitialized value $_ in pattern match (m//) at
/vcs/d0/home/reader/scripts/perl/logwrp line 48, <> line 1.
Use of uninitialized value $_ in pattern match (m//) at
/vcs/d0/home/reader/scripts/perl/logwrp line 48, <> line 2.
Use of uninitialized value $_ in pattern match (m//) at
/vcs/d0/home/reader/scripts/perl/logwrp line 48, <> line 3.
Use of uninitialized value $_ in pattern match (m//) at
/vcs/d0/home/reader/scripts/perl/logwrp line 48, <> line 4.
Use of uninitialized value $_ in pattern match (m//) at
/vcs/d0/home/reader/scripts/perl/logwrp line 48, <> line 5.
Use of uninitialized value $_ in pattern match (m//) at
/vcs/d0/home/reader/scripts/perl/logwrp line 48, <> line 6.
Use of uninitialized value $_ in pattern match (m//) at
/vcs/d0/home/reader/scripts/perl/logwrp line 48, <> line 7.
Use of uninitialized value $_ in pattern match (m//) at
/vcs/d0/home/reader/scripts/perl/logwrp line 48, <> line 8.
------- ------- ---=--- ------- -------
Log lines fed to scripts in file `logs'
------- ------- ---=--- ------- -------
cat logs
Aug 16 19:20:01 d2 sendmail[3470]: NOQUEUE: SYSERR(smmsp): QueueDirectory (Q)
option must be set
Aug 16 20:45:37 d2 sm-mta[768]: starting daemon (8.14.4): SMTP+queueing@00:10:00
Aug 16 20:45:38 d2 sm-msp-queue[788]: NOQUEUE: SYSERR(root):
/etc/mail/submit.cf: line 2: invalid argument to V line: "ERSIONID(\001Id:
submit"
Aug 16 20:45:38 d2 sm-msp-queue[788]: NOQUEUE: SYSERR(root): No local mailer
defined
Aug 16 20:45:38 d2 sm-msp-queue[788]: NOQUEUE: SYSERR(root): QueueDirectory (Q)
option must be set
Aug 16 21:34:28 d2 sendmail[842]: alias database /etc/mail/aliases rebuilt by
root
Aug 16 21:34:28 d2 sendmail[842]: /etc/mail/aliases: 13 aliases, longest 10
bytes, 144 bytes total
Aug 16 21:34:31 d2 sm-mta[877]: starting daemon (8.14.4):SMTP+queueing@00:10:00
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/