My first Perl one-line script

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'

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

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

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

  2. 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;'

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

  4. […] 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 […]

Leave a Reply

Your email address will not be published. Required fields are marked *