« 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: |
December 14th, 2005 at 2:26 pm
You can use
Perl -peand get rid off thewhileloop. Like this:tail -f somefile | Perl -pe 's/some/other/g'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;'
December 15th, 2005 at 1:39 am
You don’t even need that last
printstatement in your code. The results will be printed out anyway.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 [...]