Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I grep and sort text files using Perl?

Tags:

text

grep

perl

I have a simple log file which is very messy and I need it to be neat. The file contains log headers, but they are all jumbled up together. Therefore I need to sort the log files according to the log headers. There are no static number of lines - that means that there is no fixed number of lines for the each header of the text file. And I am using perl grep to sort out the headers.

The Log files goes something like this:

Car LogFile Header
<text>
<text>
<text>
Car LogFile Header
<text>
Car LogFile Header
<and so forth>

I have done up/searched a simple algorithm but it does not seem to be working. Can someone please guide me? Thanks!

#!/usr/bin/perl

#use 5.010; # must be present to import the new 5.10 functions, notice 
#that it is 5.010 not 5.10


my $srce = "./root/Desktop/logs/Default.log";
my $string1 = "Car LogFile Header";
open(FH, $srce);
my @buf = <FH>;
close(FH);
my @lines = grep (/$string1/, @buffer);

After executing the code, there is no result shown at the terminal. Any ideas?

like image 760
JavaNoob Avatar asked Sep 24 '10 02:09

JavaNoob


2 Answers

I think you want something like:

 my $srce = "./root/Desktop/logs/Default.log";
 my $string1 = "Car LogFile Header";

 open my $fh, '<',  $srce or die "Could not open $srce: $!";

 my @lines = sort grep /\Q$string1/, <$fh>;
 print @lines;

Make sure you have the right file path and that the file has lines that match your test pattern.

It seems like you are missing a lot of very basic concepts and maybe cutting and paste code you see elsewhere. If you're just starting out, pick up a Perl tutorial such as Learning Perl. There are other books and references listed in perlfaq2.

like image 144
brian d foy Avatar answered Nov 06 '22 02:11

brian d foy


Always use:

use strict;
use warnings;

This would have told you that @buffer is not defined.

#!/usr/bin/perl

use strict;
use warnings;

my $srce = "./root/Desktop/logs/Default.log";
my $string1 = "Car LogFile Header";
open(my $FH, $srce) or die "Failed to open file $srce ($!)";
my @buf = <$FH>;
close($FH);
my @lines = grep (/$string1/, @buf);
print @lines;

Perl is tricky for experts, so experts use the warnings it provides to protect them from making mistakes. Beginners need to use the warnings so they don't make mistakes they don't even know they can make.

(Because you didn't get a chance to chomp the input lines, you still have newlines at the end so the print prints the headings one per line.)

like image 31
Jonathan Leffler Avatar answered Nov 06 '22 03:11

Jonathan Leffler