Re: How to read each line of file into an array and split contnts of each line into array cells
by NetWallah (Canon) on Sep 30, 2013 at 06:28 UTC
|
perl -F-\|@\|~ -anE 'say for @F; push @x, {TIME=>$F[3],REC=>$_,PIECES=
+>[@F]}}{for (@x){next unless $_->{TIME} ge "06" and $_->{TIME} le "1
+8";say qq(do someting with $_->{REC}) }' <Your file name>
My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.
| [reply] [d/l] |
Re: How to read each line of file into an array and split contnts of each line into array cells
by CountZero (Bishop) on Sep 30, 2013 at 06:19 UTC
|
Show us what you have tried (even if it failed).
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics
| [reply] |
|
#!/usr/bin/perl;
use strict;
use warnings;
use Getopt::Long;
my @ARRAY = ();
my $ARRAYlength = -1;
my $FILEname="/var/log/logfile.txt";
open FILE, $FILEname or die "couldn't open $FILEname: $!";
while (<FILE>)
{
@ARRAY = <FILE>;
print "@ARRAY\n"
}
close FILE or die "problems closing $FILEname: $!";,<
Output of above script:
**FAILED PROD** - BUGS - 08/26/13 @11:12 - BUGS~23AUG13~23AUG13~23AUG13~26AUG13~1109
**FAILED PROD** - cockroach - 08/29/13 @11:52 -cockroach~29AUG13~29AUG13~29AUG13~29AUG13~1152
Iam able to read the logfile lines into array and at this point I want to split components of each line and check the time and alert if it is between 18hrs to 6AM following day. | [reply] [d/l] |
Re: How to read each line of file into an array and split contnts of each line into array cells
by wjw (Priest) on Sep 30, 2013 at 06:34 UTC
|
I would guess you can split the lines without help as that is very basic.
Once you have done that, look at Date::Calc to help you determine the appropriateness of any given entry. Your date field will need to be moderately massaged to work, but that is also very basic.
Just look at Date::Calc on CPAN and read it... all will become apparent...
- ...the majority is always wrong, and always the last to know about it...
- ..by my will, and by will alone.. I set my mind in motion
| [reply] [d/l] |
Re: How to read each line of file into an array and split contnts of each line into array cells
by hdb (Monsignor) on Sep 30, 2013 at 11:06 UTC
|
You do not really need to split the lines to find the time. Extract it with a regex and then print the line unless the time is within 6 hours either side of noon:
use strict;
use warnings;
abs( 1200 - join "", /(\d\d):(\d\d)/ ) < 600 or print while(<DATA>);
__DATA__
**FAILED PROD** - BUGS - 08/26/13 @05:12 - BUGS~23AUG13~23AUG13~23AUG1
+3~26AUG13~
**FAILED PROD** - BUGS - 08/26/13 @11:12 - BUGS~23AUG13~23AUG13~23AUG1
+3~26AUG13~1109
**FAILED PROD** - cockroach - 08/29/13 @11:52 -cockroach~29AUG13~29AUG
+13~29AUG13~29AUG13~1152
**FAILED PROD** - cockroach - 08/29/13 @18:00 -cockroach~29AUG13~29AUG
+13~29AUG13~29AUG13~1152
| [reply] [d/l] |
|
"...You do not really need to split the lines to find the time. Extract it with a regex and then print the line..."
And split doesn't use regex shea? of course I know it does uses
| [reply] |
|
In my response, there is no reference to Perl's split. Besides being a popular function in Perl, split is also an English verb. Most likely split got its name from split. Now, splitting strings into parts in Perl is often done using split but can be achieved in numerous other ways. All I wanted to say is, that the retrieval of the time is possible by applying a regular expression to the complete line from the log file without splitting it first.
| [reply] |
|
split might use regex.
The statement is still correct: You don't need to use split.
| [reply] |
Re: How to read each line of file into an array and split contnts of each line into array cells
by kcott (Archbishop) on Sep 30, 2013 at 13:30 UTC
|
G'day crazy-duck,
Welcome to the monastery.
You should set up dummy data to test your solution.
That data should include boundary conditions (e.g exactly 6am, 1 minute before, and 1 minute after).
You also need to be clearer about the range: "between 6pm to 6am" inclusive? exclusive? other?
In the code you posted, there's some problems:
-
See the open documentation for the preferred way to open a file for reading.
Something like: open my $fh, '<', $filename ...
-
while (<FILE>) (which would become while (<$fh>)) reads a record at a time.
Given you're reading a (potentially very long) log file, this is a good idea.
-
After reading the first record, you read all the remaining records into an array with @ARRAY = <FILE>.
This is a bad idea for the same reason that reading a record at a time was a good idea.
Also, as there's now no more records to read, the while loop won't reiterate (i.e. in total, it only loops once).
-
There's some garbage in the close FILE line. Possibly a typo; however, you need to post exactly the code you're running: we can't guess what you meant to write. See the close documentation, if necessary.
Identifying the lines you want is fairly straightforward.
For something this simple, I wouldn't recommend a CPAN module nor a complex calculation.
A plain comparison, which is easy to read and, if necessary, easy to adjust, is all that's required.
#!/usr/bin/env perl
use strict;
use warnings;
while (<DATA>) {
my ($time, $hours, $mins) = /@((\d\d):(\d\d))/;
next unless $hours < 6 || $hours > 17 || $time eq '06:00';
# A line we want is in $_ - process it here, e.g.
print;
}
__DATA__
**FAILED PROD** - BUGS - 08/26/13 @00:00 - ... WANTED 1
**FAILED PROD** - BUGS - 08/26/13 @00:01 - ... WANTED 2
**FAILED PROD** - BUGS - 08/26/13 @05:59 - ... WANTED 3
**FAILED PROD** - BUGS - 08/26/13 @06:00 - ... WANTED 4
**FAILED PROD** - BUGS - 08/26/13 @06:01 - ... NOT WANTED 1
**FAILED PROD** - BUGS - 08/26/13 @12:00 - ... NOT WANTED 2
**FAILED PROD** - BUGS - 08/26/13 @17:59 - ... NOT WANTED 3
**FAILED PROD** - BUGS - 08/26/13 @18:00 - ... WANTED 5
**FAILED PROD** - BUGS - 08/26/13 @18:01 - ... WANTED 6
**FAILED PROD** - BUGS - 08/26/13 @23:59 - ... WANTED 7
**FAILED PROD** - BUGS - 08/26/13 @24:00 - ... WANTED 8 (LAST)
Output:
**FAILED PROD** - BUGS - 08/26/13 @00:00 - ... WANTED 1
**FAILED PROD** - BUGS - 08/26/13 @00:01 - ... WANTED 2
**FAILED PROD** - BUGS - 08/26/13 @05:59 - ... WANTED 3
**FAILED PROD** - BUGS - 08/26/13 @06:00 - ... WANTED 4
**FAILED PROD** - BUGS - 08/26/13 @18:00 - ... WANTED 5
**FAILED PROD** - BUGS - 08/26/13 @18:01 - ... WANTED 6
**FAILED PROD** - BUGS - 08/26/13 @23:59 - ... WANTED 7
**FAILED PROD** - BUGS - 08/26/13 @24:00 - ... WANTED 8 (LAST)
| [reply] [d/l] [select] |
|
| [reply] |
|
"Thank you for your response and appreciate all your efforts in helping here,"
You're welcome.
"for the above script can we amend to do a text comparision "**FAILED PROD**" that comes in between 6pm to 6am inclusive."
I don't understand this:
-
I don't know who you are referring to when you say "we".
-
If you're asking for some sort of permission to use/modify this code, you don't need to.
-
The code is already dealing with "6pm to 6am inclusive".
| [reply] |
|
|