« Foto: Moscow view | Home | Inside del.icio.us »

My first Perl one-line script

By Michael Stepanov | December 14, 2005

Once I found cool articles about using Perl in command line. Today I created my own one line script. By some reason Apache 2 escape \n and \t in its log:

tail -f /var/log/httpd/videoguide-error_log


[Tue Dec 13 23:46:26 2005] [error] [4401]ERR: 24: Error in Perl code: VG::Users can’t SELECT id\nFROM Users\nWHERE login = ?\n: DBD::mysql::st execute failed: Unknown column ‘login’ in ‘where clause’ [for Statement "SELECT id\nFROM Users\nWHERE login = ?\n"] at /usr/lib/perl5/site_perl/5.8.6/DBIx/ContextualFetch.pm line 51.

It’s very difficult to read this information and I didn’t find information how to fix it on Apache level. Instead I use Perl:

tail -f /var/log/httpd/videoguide-error_log | Perl -e 'while(<>) { s#\\n#\n#g; s#\\t#\t#g; print; }’

and result is very good:

[Tue Dec 13 23:46:26 2005] [error] [4401]ERR: 24: Error in Perl code: VG::Users can’t SELECT id
FROM Users
WHERE login = ?
: DBD::mysql::st execute failed: Unknown column ‘login’ in ‘where clause’ [for Statement "SELECT id
FROM Users
WHERE login = ?
"] at /usr/lib/perl5/site_perl/5.8.6/DBIx/ContextualFetch.pm line 51.

UPDATED: the latest version one-liner:

Once I found cool articles about using Perl in command line. Today I created my own one line script. By some reason Apache 2 escape \n and \t in its log:

tail -f /var/log/httpd/videoguide-error_log


[Tue Dec 13 23:46:26 2005] [error] [4401]ERR: 24: Error in Perl code: VG::Users can’t SELECT id\nFROM Users\nWHERE login = ?\n: DBD::mysql::st execute failed: Unknown column ‘login’ in ‘where clause’ [for Statement "SELECT id\nFROM Users\nWHERE login = ?\n"] at /usr/lib/perl5/site_perl/5.8.6/DBIx/ContextualFetch.pm line 51.

It’s very difficult to read this information and I didn’t find information how to fix it on Apache level. Instead I use Perl:

tail -f /var/log/httpd/videoguide-error_log | Perl -pe ' s#\\n#\n#g; s#\\t#\t#g'

Topics: |

 RSS feed for comments on this post.

4 Responses to “My first Perl one-line script”

  1. Leonid Mamchenkov Says:
    December 14th, 2005 at 2:26 pm

    You can use Perl -pe and get rid off the while loop. Like this:

    tail -f somefile | Perl -pe 's/some/other/g'

  2. Michael Stepanov Says:
    December 14th, 2005 at 2:51 pm

    You are completely right. I forgot about this command line option :)
    The new version is:


    tail -f /var/log/httpd/videoguide-error_log | Perl -pe 's#\\n#\n#g; s#\\t#\t#g; print;'

  3. Leonid Mamchenkov Says:
    December 15th, 2005 at 1:39 am

    You don’t even need that last print statement in your code. The results will be printed out anyway. ;)

  4. Michael’s Tech Blog » Blog Archive » Perl Oneliner: Recursive Search and Replace Says:
    February 15th, 2007 at 11:36 pm

    [...] It’s cool to make a simple scripts in one line to do some routine work. For example, my first Perl oneliner formats Apache log to be easy readable. Here is a [...]

Comments