« Earlier 8 items total Later »

On this page: 

Measure the daily number of E-mail messages in a mailbox

This snippet written in bash with calls to perl from the command line measures the number of E-mail messages sent to a mailbox per calendrical day.

#!/bin/bash
grep -h '^Date:' * |
    perl -pe 's!^Date: !!' |
    perl -pe 's!^\w\w\w, !!' |
    perl -pe 's{\d{2}:\d{2}:\d{2}.*$}{}' |
    perl -pe 's!^\s+!!' |
    perl -pae '$_=sprintf("%.2d-%s-%s\n", @F)' |
    sort | uniq -c | sort -n


I used perl for some places where sed would have been more suitable because I find the sed regexp syntax confusing. :-)

Pretty darn good Email validator

This regular expression was written by a friend of mine according to the email spec RFC2822. It should be near 100% coverage of email addresses assuming no leading or trailing whitespace, and no internal linebreaks (you might try 's' at the end to remedy the line break thing but no guarantees).

/^[-^!#$%&'*+\/=?`{|}~.\w]+@[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])*(\.[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])*)+$/

Extract hrefs and link text from html

while (/href=(["']?)([\/:a-zA-Z0-9%_\-+.,:!@#$%^&*()\[\]<>?=]+?)\1>(.*?)</ig) {
        print "$2 -> $3\n";
    }

Perl find and replace

perl -i.bak -pe "s/Old text/New text/g" file1 file2 file3


-i: the extension to append to the backup of the original file (omit the .bak for no backup)

Improved admin_tools for Scoop

If you've ever built a site with Scoop, you know that all that messy hard-coded table-based HTML can be a pain. In the course of building several Scoop sites I've been reworking some of the common boxes to use cleaner, easily-styled, semantic markup, and this seems as good a place as any to post the code.

Here's a reworked admin_tools which outputs as an unordered list (and puts the "edit user" form in a definition list):

my $content;
my @tools = sort { $a->{pos} <=> $b->{pos} } values %{ $S->{ADMIN_TOOLS} };
foreach my $t (@tools) {
        if ( $S->have_perm($t->{perm}) ) {
                $content .= qq\|
        <LI><A href="|rootdir|/admin/$t->{tool}">$t->{menuname}</A></LI>\|;
        }
}
if ($S->have_perm('edit_user')) {
 $content .= qq{<LI>
 <FORM NAME="uedit" METHOD="GET" ACTION="|rootdir|/">
 <DL>
 <DT>Edit User:</DT>
 <DD>
 <INPUT TYPE="hidden" NAME="op" VALUE="user">
 <INPUT TYPE="text" NAME="nick" VALUE="" SIZE=10>
 <INPUT TYPE="SUBMIT" NAME="tool" VALUE="prefs">
 <INPUT TYPE="SUBMIT" NAME="tool" VALUE="info">
 </DD>
 </DL>
 </FORM>
 </LI>};
}
return '' unless $content;
return{content=>qq{<UL>$content</UL>}};

Improved user_box for Scoop

If you've ever built a site with Scoop, you know that all that messy hard-coded table-based HTML can be a pain. In the course of building several Scoop sites I've been reworking some of the common boxes to use cleaner, easily-styled, semantic markup, and this seems as good a place as any to post the code.

Here's a reworked user_box which outputs the tools menu as an unordered list:

my $content;
if ($S->{UID} > 0) {
  if ($S->have_perm('moderate')) {
    my ($nstories, $tstories) = $S->_count_new_sub();
    my $color = '';
    my $c_end = '';
    if ($nstories) {
       $color = '<strong style="color: #f00;">';
       $c_end = '</strong>';
    }
    $content = qq\|<ul>
    <li><a href="|rootdir|/modsub">Vote on Submissions</a> ($tstories/$color$nstories$c_end new)</li>\|;
  }

  if ($S->{TRUSTLEV} == 2 \|\| $S->have_perm('super_mojo')) {
    $content .= qq{<li><a href="|rootdir|/search?type=comment;hidden_comments=show">Review Hidden Comments</a></li>};
  }
  
  my $urlnick = $S->urlify($S->{NICK});
  my $diary_link = $S->{UI}->{VARS}->{use_diaries} ? 
                qq{
      <li><a href="|rootdir|/user/$urlnick/diary">Your Diary</a></li>
      <li><a href="|rootdir|/submitstory/Diary">New Diary Entry</a></li> 
                } : '';
        my $upload_link = ($S->{UI}->{VARS}->{allow_uploads} && ($S->have_perm('upload_user') \|\| $S->have_perm('upload_admin'))) ?
                qq{<li><a href="|rootdir|/user/$urlnick/files">Your Files</a></li>} : '';

        my $ad_link = $S->{UI}->{VARS}->{use_ads} ?
                qq{
      <li><a class="light" href="|rootdir|/user/$urlnick/ads">Your Advertisements</a></li>
          <li><a href="|rootdir|/submitad">Submit Ad</a></li>
                } : '';

  $content .= qq\|
      <li><a href="|rootdir|/user/$urlnick">User Info</a></li>
      <li><a href="|rootdir|/user/$urlnick/comments">Your Comments</a></li>
      <li><a href="|rootdir|/user/$urlnick/stories">Your Stories</a></li>
      $diary_link
          $ad_link
      $upload_link
      <li><a href="|rootdir|/user/$urlnick/prefs">User Preferences</a></li>
      <li><a href="|rootdir|/my/prefs/Interface">Display Preferences</a></li>
      <li><a href="|rootdir|/my/prefs/Comments">Comment Preferences</a></li>
      <li><a href="|rootdir|/logout">Logout $S->{NICK}</a></li></ul>\|;

    $title = $S->{NICK};
} else {
    $content = $S->{UI}->{BLOCKS}->{login_box};
    $content =~ s/|LOGIN_ERROR|/$S->{LOGIN_ERROR}/;
}
return {content => $content, title => $title };

Parse comma separated data

Import data in CSV format. Normally an easy task using split but sometimes, the fields will have comma's enclosed by quotes.
Something like.
Aaron, "123 Main St, USA", 555-555-1212
Assuming your string gets put into $text.
    my @new  = ();
    push(@new, $+) while $text =~ m{
        # the first part groups the phrase inside the quotes.
        # see explanation of this pattern in MRE
        "([^\"\\]*(?:\\.[^\"\\]*)*)",?
           |  ([^,]+),?
           | ,
       }gx;
       push(@new, undef) if substr($text, -1,1) eq ',';

Taken from Jeffrey Friedl's Mastering Regular Expressions

Encode and Decode strings from URL.

Encode a string so that it's safe to be passed in a url. Equivalent of urlencode in PHP.
$str =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;


Now, the reverse, decode a string from a url. Equivalent of urldecode in PHP.
$str =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;

« Earlier 8 items total Later »