1

I'm new to perl. I have a file which i need to parse using perl script.

The format of the file is as below:

05\/26\/2013 06:09:47 \-0700 - AUTHN_SUCCESS - GET - ddsbcggio_ac  - 200.12.33.44 - abcweb.eegeserv.com\/abcweb\/abcwebInitialize.do?PORT=SPQ  - [email protected]\,ou=People\,o=zeb.com - 06:09:47 - http - uizweb_zam -  - [email protected]
05\/26\/2013 06:09:48 \-0700 - AUTHN_SUCCESS - GET - ddsbcggio_ac  - 200.12.33.44 - abcweb.eegeserv.com\/abcweb\/abcwebInitialize.do?PORT=SPQ  - [email protected]\,ou=People\,o=zeb.com - 06:09:48 - http - uizweb_zam -  - [email protected]
05\/26\/2013 06:09:49 \-0700 - AUTHN_SUCCESS - GET - ddsbcggio_ac  - 200.12.33.43 - abcweb.eegeserv.com\/abcweb\/abcwebInitialize.do?PORT=SPQ  - [email protected]\,ou=People\,o=zeb.com - 06:09:49 - http - uizweb_zam -  - [email protected]

From the file i want to get something like these:

05/26/2013 06:09:49  and [email protected],ou=People,o=zeb.com

from each line.

I've tried split(), but i'm not able to get it properly.

Please help

2
  • 1
    Why would split not work? Commented May 29, 2013 at 5:48
  • If split won't work try unpack for fun then. Commented May 29, 2013 at 5:55

5 Answers 5

1

Simple script with split

my $IP_FILE=$ARGV[0];
open(IP_FILE,$IP_FILE) || die "Unable to open file...";
while(<IP_FILE>)
{
    chomp;
    my @vals = split("-");
    $vals[0] =~ s/\\//g;
    $vals[7] =~ s/\\//g;
    printf("%s %s\n",$vals[0],$vals[7]);
}
close(IP_FILE);
Sign up to request clarification or add additional context in comments.

4 Comments

Btw can you please recommend any book or links to quickly learn perl? also i would like to learn using ldap with perl
@david The Perl website has excellent documentation and a brief introductory tutorial.
@Raghuram I have one problem now. There are some lines like: 5\/26\/2013 06:09:47 \-0700 - AUTHN_SUCCESS - GET - ddsbcggio_ac - 200.12.33.44 - abcweb.eegeserv.com\/abcweb\/abcwebInitialize.do?PORT=SPQ - [email protected]\,ou=People\,o=zeb.com - 06:09:47 - http - uizweb_zam - - [email protected] Due to this: uid=rada-sh , split is not working properly, can you please suggest a solution?
Please edit your question to add the new pattern in the file....as i am not able to make out whats the difference between old and new pattern
1
perl -ape '$_ = "$F[0] $F[1]  and $F[14]\n"; s|\\||g;' file

Comments

1
 #open file 
 open(FILE, "FILENAME.txt")  || die "Can't open file";

 # read file into an array 
 @data = <FILE>;
 close(FILE);
 foreach $line(@data) {

 @output = split / /, $line;
 $output[0] =~ s/\\//g;
 print "$output[0] $output[1] and $output[16]\n";
 }

or you can push it to use later.

 #open file 
 open(FILE, "FILENAME.txt")  || die "Can't open file";

 # read file into an array 
 @data = <FILE>;
 close(FILE);
 foreach $line(@data) {

 @output = split / /, $line;
 $output[0] =~ s/\\//g;
 push @result, "$output[0] $output[1] and $output[16]\n";
 }
 print @result;

Not very clean code, because if the file's data ever moves to other locations within the file, it will give incorrect results, but this works and you'll get the idea of how the split works.

As for your question on links. Here is a good beginners eBook.

  http://bookboon.com/en/perl-for-beginners-ebook

1 Comment

@david here is a good book to start with. it is free! bookboon.com/en/perl-for-beginners-ebook
0
perl -lane '$a="@F[0,1]";$a=~s/[\\\/]//g;$F[14]=~s/\\//g;print "$a and $F[14]"' your_file

Comments

0

If you know the delimiter, - you can use Text::ParseWords to parse it. It is a core module in Perl 5. I supply the regex \s*-\s* to strip the whitespace around the dashes.

use strict;
use warnings;
use Text::ParseWords;
use feature 'say';

while (<DATA>) {
    chomp;
    my @data = quotewords('\s*-\s*', 0, $_);
    say join " ", @data[0, 6];
}


__DATA__
05\/26\/2013 06:09:47 \-0700 - AUTHN_SUCCESS - GET - ddsbcggio_ac  - 200.12.33.44 - abcweb.eegeserv.com\/abcweb\/abcwebInitialize.do?PORT=SPQ  - [email protected]\,ou=People\,o=zeb.com - 06:09:47 - http - uizweb_zam -  - [email protected]
05\/26\/2013 06:09:48 \-0700 - AUTHN_SUCCESS - GET - ddsbcggio_ac  - 200.12.33.44 - abcweb.eegeserv.com\/abcweb\/abcwebInitialize.do?PORT=SPQ  - [email protected]\,ou=People\,o=zeb.com - 06:09:48 - http - uizweb_zam -  - [email protected]
05\/26\/2013 06:09:49 \-0700 - AUTHN_SUCCESS - GET - ddsbcggio_ac  - 200.12.33.43 - abcweb.eegeserv.com\/abcweb\/abcwebInitialize.do?PORT=SPQ  - [email protected]\,ou=People\,o=zeb.com - 06:09:49 - http - uizweb_zam -  - [email protected]

Output:

05/26/2013 06:09:47 -0700 [email protected],ou=People,o=zeb.com
05/26/2013 06:09:48 -0700 [email protected],ou=People,o=zeb.com
05/26/2013 06:09:49 -0700 [email protected],ou=People,o=zeb.com

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.