0

I would like to parse a file containing data 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]

and get:

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

I tried split('-') but it does not work split('-') because as you can see : Some lines like the second one above has: [email protected] ( a '-' ) in between. Also some times, there are '-' in other part of data too.

Please help.

6
  • 3
    how about split / - / with spaces around the hyphen? Commented Jun 5, 2013 at 16:57
  • 1
    Do the backslashes actually exist in the data? Commented Jun 5, 2013 at 17:01
  • @amon I'll try. Presently i'm using split("-",$line), not how shall i change it to have space? Commented Jun 5, 2013 at 17:02
  • @Borodin Yes , they exist in the data Commented Jun 5, 2013 at 17:02
  • On the second line after "and get", you had backslash in front of ,ou and ,o, is that a typo? Commented Jun 5, 2013 at 17:25

2 Answers 2

1

You maybe better off using a regular expression. With a regular expression, I can quickly grab the part of the string I want with (...). See the Perldoc on Regular expressions to see what the various regular expression meta-characters mean.

#! /usr/bin/env perl

use 5.12.0;
use warnings;
use autodie;

while ( my $line = <DATA> ) {
    chomp $line;
    $line =~ s/\\//g;   #Remove all backslashes
    $line =~ /^(.+?) -.+?(uid=\S+)/;
    my $date = $1;
    my $uid = $2;
    say qq($date and $uid);
}

__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]
Sign up to request clarification or add additional context in comments.

Comments

0

This program does what you ask. It looks like the field separator is ' - ', i.e. a hyphen with a space either side, giving a blank penultimate field (the eleventh).

This program expects the name of the input file as a parameter on the command line.

use strict;
use warnings;

while (<>) {
  chomp;
  tr/\\//d;
  my @fields = split /\x20-\x20/;
  printf "%s and %s\n", @fields[0,6];
}

With your own data, this produces

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

5 Comments

What if the file has: uid=rad - [email protected],ou=People,o=zeb.com. Will this work?
No. You are asking too much. Is that likely? Or even possible?
If they are valid email addresses then they cannot contain whitespace.
Yes, i have something like [email protected],ou=Peo - ple,o=zeb.com. Please note ou=Peo - ple
@david: Then please describe how to distinguish between " - " as a separator and the same thing within a field.

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.