• In this little script, we take the Apache access_log file, and read it into an array. Each element of the array is split on the space (" "), and we print out the first element, which is the IP address of the machine connecting to our website.

    Here is the format for the log file:


    69.47.145.197 - - [15/Jan/2006:22:35:12 -0500] "GET / HTTP/1.1" 301 354
    69.47.145.197 - - [15/Jan/2006:22:35:39 -0500] "GET /content/images/oa1.jpg HTTP/1.1" 301 376
    69.47.145.197 - - [15/Jan/2006:22:35:40 -0500] "GET / HTTP/1.1" 301 354
    202.7.166.167 - - [16/Jan/2006:00:02:41 -0500] "GET /content/images/oa1.jpg HTTP/1.0" 301 376
    202.7.166.167 - - [16/Jan/2006:00:06:23 -0500] "GET /content/images/oa1.jpg HTTP/1.0" 301 376
    66.249.64.14 - - [16/Jan/2006:00:14:08 -0500] "GET /robots.txt HTTP/1.0" 301 364

    PERL:
    1. #!/usr/bin/perl
    2. open (FD, "/path/to/your/access_log");
    3. while (<fd>)
    4. {
    5. @array = split (/[" "]/);
    6. foreach $i (@array)
    7.         {
    8.         print $array[0] . "\n";
    9.         }
    10. }
    11. close (FD);

    In this example we use the special variable "$_". This is used to hold data from the file without having to do an explicit read operation. If we wanted to split the lines on other characters we could do something like this:

    @array = split (/[-,:," "]/);

    This would split it on the dash "-", colon ":" and the space " ".

    If we wanted to read out each line as it is read in, we could simply replace:

    print $array[0] . "\n";

    With:

    print $i . "\n";

    This entry was posted on Monday, July 10th, 2006 at 4:51 pm and is filed under Data and Technology. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
  • 5 Comments

    Take a look at some of the responses we've had to this article.

    1. benizi
      Dec 9th
      Reply

      It looks like you’re coming from a PHP background (based on seeing how others who do that do things in Perl). It’s pretty odd to use the ‘.’ (concatenation) operator when you just want to interpolate a variable.

      print $array[0] . "\n";

      is usually written:

      print "$array[0]\n";

      And you’re doing something odd in the foreach loop. (You’ll start to abbreviate that to ‘for’ if you continue with Perl — the two are synonymous even though there are two forms of “what to loop over”.) Why are you doing:

      for $i (@array) { print $array[0] . "\n"; }

      That’ll give you as many copies of $array[0] as there are parts in the array.

      You can accomplish everything you’re doing in this file with:

      perl -lanwe 'print $F[0]' /path/to/your/access_log

      For text processing things, it’s often better to use (with no explicit file-opening/-closing) than .

      Just some tips for someone who might come across this later, as I did.

    2. benizi
      Dec 9th
      Reply

      (In the previous comment, the code tags ate my file operator:)

      <> is better than <FD>

    3. benizi
      Dec 9th
      Reply

      Oh, and really, text-processing that’s this straight-forward is usually better done with ‘cut’:

      cut -f1 -d' ' /path/to/access_log

      (But, if you want to do something more complicated, it’s nice to have the scaffolding that you have:)


      #!/usr/bin/perl
      while (<>) {
      my @array = split;
      # do something with @array here
      print "$array[0]\n";
      }

    4. Stefhen
      Dec 12th
      Reply

      awk ‘{print $1}’ /path/to/your/access_log will do the same thing.

    5. Dec 13th
      Reply

      @Benzi & Stefhen,

      Thanks for the tips… These little snips are notes to myself from my adventures trying to teach myself perl, so having your pointers really helps. I tend to like using the UNIX tools like cut, sed and awk better as well because I’m much more familiar with them.

  • Leave a Reply

    Let us know what you thought.

  • Name (required):

    Email (required):

    Website:

    Message:

Visitors have tagged this post: t (177) - perl cut (113) - perl colon (83) - perl cut string (74) - perl split array (55) - perl split colon (49) - perl split on first space (37) - perl cut text (33) - perl concatenate files (32) - cut string perl (30) - perl split space (27) - bash split path (22) - perl print space (22) - perl =cut (21) - perl concatenate lines (21) - colon perl (20) - perl split dash (19) - perl split on colon (19) - colon in perl (19) - concatenate two files in perl (19) - perl split first (19) - cut string in perl (16) - perl concatenate two files (15) - perl print array to file (14) - perl string cut (14) - cut in perl (13) - perl cut space (12) - perl split first space (11) - perl cut equivalent (11) - perl cut array (11) - perl split text (11) - perl cut spaces (11) - concatenate two lines in perl (11) - f (10) - perl split path (10) - perl array split (10) - cut perl (10) - perl cut column (10) - perl access_log (9) - perl split on first (9) - split array perl (9) - perl colon operator (9) - perl split on dash (8) - perl cut split (8) - perl split by colon (8) - perl string cutting (8) - perl print array (7) - perl split on space (7) - perl cut file (7) - Perl splitpath (7) - perl array concatenation (7) - awk split on colon (7) - perl cut strings (7) - perl cut string into array (7) - perl concat files (7) - perl split only first (7) - PERL split string (7) - print space perl (7) - Perl Lessons (6) - perl ($ split first (6) - bash split (6) - perl array cut (6) - perl append two files (6) - perl concatenate array (6) - perl print spaces (6) - print space in perl (6) - bash split array (5) - perl split cut (5) - split space perl (5) - perl split awk (5) - perl split file (5) - awk split by colon (5) - bash split string into array (5) - perl split filepath (5) - perl concatenate text files (5) - awk split path (5) - perl split only on first space (5) - split on space perl (5) - perl concatenation loop (5) - how to concatenate two files in perl (5) - concatenate lines in perl (5) - cut a string in perl (5) - concatenate files perl (5) - perl split hyphen (5) - concatenate lines perl (5) - perl split access_log (4) - bash split colon (4) - perl split on * (4) - perl split [1] (4) - perl print array file (4) - bash array space (4) - awk split colon (4) - perl split by space (4) - perl file path space (4) - split on colon perl (4) - bash split space (4) - cut equivalent in perl (4) - bash concat text (4) - perl split an array (4) - bash concat (4) - perl print dash (4) - perl cut a string (4) - perl to concatenate 2 lines (4) - perl file concatenation (4) - perl print array split (4) - concatenating two files in perl (4) - cut en perl (4) - how to cut a string in perl (4) - perl split print array (4) - perl concat two lines (4) - perl array howto (3) - perl split 2 spaces (3) - bash split examples (3) - perl split at first (3) - bash array split (3) - perl cut first space (3) - printing spaces in perl (3) - awk 2 perl (3) - bash cut space (3) - perl split file path (3) - perl split sample (3) - perl colons (3) - cut equivalent perl (3) - perl string to array (3) - cut path perl (3) - concatenate space in perl (3) - perl concat (3) - concat in perl (3) - print spaces perl (3) - cut string perl script (3) - perl concatenate path (3) - perl cut first line (3) - cut text in perl (3) - bash split on space (3) - perl cut -c (3) - perl array partition (3) - perl concat space (3) - concat two lines in perl (3) - split on first space perl (3) - perl cut path (3) - perl concat two files (3) - perl array space (3) - perl cut characters (3) - split path string into 2 columns (3) - concatenate two files perl (3) - perl concatenate two lines (3) - concatenate two lines perl (3) - concatenating lines perl (3) - how to print space in perl (3) - concat 2 lines in perl (3) -