vdthoughts has asked for the wisdom of the Perl Monks concerning the following question:

I would like to extract the user ID's only from the log file. I have copied the sample code below. All the user ID's are inbetween the text "ID principal: " & ", ". Later i would like to use the data to be copied into an excel sheet to create a chart. Can anybody help me to write a perl script for the same. I have never done some complex task on perl prog till now. --------------------------------------------------------- Sample log file code: Tue Nov 5 09:00:00 2013 SI Floating Licenses: 14 ID principal: IDB2COB, ID principal: kdk4oe, ID principal: oij7nde, ID principal: qay8plm, ID principal: wsx9okn, ID principal: LNA6COB, ID principal: lak3kor, ID principal: deveas SI Seat Licenses: 7 ID principal: ISN1COB, ID principal: kao7wbe, ID principal: kor4sds, ID principal: HSC4WE, ID principal: usr4cint, ID principal: SUU4COB, ID principal: birr IM Floating Licenses: 8 ID principal: tram2we, ID principal: IDB2COB, ID principal: kao7wbe, ID principal: HSC4WE, ID principal: VBN1KOR, ID principal: adf3rtz, ID principal: MAG4KOR, ID principal: ASI3COB IM Seat Licenses: 2 ID principal: kor4sds, ID principal: ribbs Tue Nov 5 09:30:00 2013 SI Floating Licenses: 10 ID principal: IDB2COB, ID principal: kdk4oe, ID principal: oij7nde, ID principal: qay8plm, ID principal: wsx9okn, ID principal: LNA6COB, ID principal: lak3kor, ID principal: deveas SI Seat Licenses: 7 ID principal: ISN1COB, ID principal: kao7wbe, ID principal: kor4sds, ID principal: HSC4WE, ID principal: usr4cint, ID principal: SUU4COB, ID principal: birr IM Floating Licenses: 8 ID principal: tram2we, ID principal: IDB2COB, ID principal: kao7wbe, ID principal: HSC4WE, ID principal: VBN1KOR, ID principal: adf3rtz, ID principal: MAG4KOR, ID principal: ASI3COB IM Seat Licenses: 2 ID principal: kor4sds, ID principal: ribbs Tue Nov 5 10:00:00 2013 SI Floating Licenses: 7 ID principal: IDB2COB, ID principal: kdk4oe, ID principal: oij7nde, ID principal: qay8plm, ID principal: wsx9okn, ID principal: LNA6COB, ID principal: lak3kor, ID principal: deveas SI Seat Licenses: 7 ID principal: ISN1COB, ID principal: kao7wbe, ID principal: kor4sds, ID principal: HSC4WE, ID principal: usr4cint, ID principal: SUU4COB, ID principal: birr IM Floating Licenses: 8 ID principal: tram2we, ID principal: IDB2COB, ID principal: kao7wbe, ID principal: HSC4WE, ID principal: VBN1KOR, ID principal: adf3rtz, ID principal: MAG4KOR, ID principal: ASI3COB IM Seat Licenses: 2 ID principal: kor4sds, ID principal: ribbs -----------------------------------------------------------

Excuse me for missing the markup!

  1. I would like to extract ONLY the user ID's from the log file. I have copied the sample code below.
  2. Note: All the user ID's in the code are in-between the text "ID principal: " & ", ".
  3. Later I would like to use the data to be copied into an excel sheet to create a chart.
  4. Can anybody help me to write a Perl script for the same.

Sample log file code:

Tue Nov 5 09:00:00 2013 SI Floating Licenses: 14 ID principal: IDB2COB, ID principal: kdk4oe, ID principal: oij7nde, +ID principal: qay8plm, ID principal: wsx9okn, ID principal: LNA6COB, +ID principal: lak3kor, ID principal: deaves SI Seat Licenses: 7 ID principal: ISN1COB, ID principal: kao7wbe, ID principal: kor4sds, + ID principal: HSC4WE, ID principal: usr4cint, ID principal: SUU4COB, + ID principal: birr IM Floating Licenses: 8 ID principal: tram2we, ID principal: IDB2COB, ID principal: kao7wbe, + ID principal: HSC4WE, ID principal: VBN1KOR, ID principal: adf3rtz, +ID principal: MAG4KOR, ID principal: ASI3COB IM Seat Licenses: 2 ID principal: kor4sds, ID principal: Gibbs Tue Nov 5 09:30:00 2013 SI Floating Licenses: 10 ID principal: IDB2COB, ID principal: kdk4oe, ID principal: oij7nde, +ID principal: qay8plm, ID principal: wsx9okn, ID principal: LNA6COB, +ID principal: lak3kor, ID principal: deaves SI Seat Licenses: 7 ID principal: ISN1COB, ID principal: kao7wbe, ID principal: kor4sds, + ID principal: HSC4WE, ID principal: usr4cint, ID principal: SUU4COB, + ID principal: birr IM Floating Licenses: 8 ID principal: tram2we, ID principal: IDB2COB, ID principal: kao7wbe, + ID principal: HSC4WE, ID principal: VBN1KOR, ID principal: adf3rtz, +ID principal: MAG4KOR, ID principal: ASI3COB IM Seat Licenses: 2 ID principal: kor4sds, ID principal: Gibbs Tue Nov 5 10:00:00 2013 SI Floating Licenses: 7 ID principal: IDB2COB, ID principal: kdk4oe, ID principal: oij7nde, +ID principal: qay8plm, ID principal: wsx9okn, ID principal: LNA6COB, +ID principal: lak3kor, ID principal: deaves SI Seat Licenses: 7 ID principal: ISN1COB, ID principal: kao7wbe, ID principal: kor4sds, + ID principal: HSC4WE, ID principal: usr4cint, ID principal: SUU4COB, + ID principal: burr IM Floating Licenses: 8 ID principal: tram2we, ID principal: IDB2COB, ID principal: kao7wbe, + ID principal: HSC4WE, ID principal: VBN1KOR, ID principal: adf3rtz, +ID principal: MAG4KOR, ID principal: ASI3COB IM Seat Licenses: 2 ID principal: kor4sds, ID principal: Gibbs

Replies are listed 'Best First'.
Re: Extract specific data (user ID) from log file
by ww (Archbishop) on Nov 05, 2013 at 13:04 UTC
    Step one: clean up the mess you've presented to us. See Writeup Formatting Tips, Markup in the Monastery and the instructions around the text-entry box re para and code tags (Hint: reopen your node and scroll down).

    Step Two: Read about regular expressions: what you need to do is very nearly as basic as exists and your answer will be found with minimal searching skills, here or in the likes of perldoc perlretut, perldoc perlrequick<c> and <c>perldoc perlre.

    Example: if you were trying to find each instance of a time inside Time: 1041; Time 913; ... end, you might want to code the job as something like this:

    my $str = "begin here Time: 1041; foo Time: 917; bar Time: fred; Time: + 2313; end"; my @arr; while ( $str =~ /Time: (\d+); /g ) { push @arr, $1; }

    Note that the perldoc references above will explain the code.

    Once you have the matches in an array, there are many XLS/Excel modules to move the data to a spreadsheet. You'll find many threads here (Super Search dealing with writing a spreadsheet. Alternately, google site: CPAN.org excel. You'll have to search the module's docs to pick an appropriate one and to learn how to complete your job/homework/whatever.

Re: Extract specific data (user ID) from log file
by marinersk (Priest) on Nov 05, 2013 at 17:34 UTC
    To help you, we'll need to know where to start.

    What's your Perl knowledge level?

    For example, do you know how to read a log file in Perl? Or would even that be a first-time task for you in Perl?