Home

Search this site:
 

Horshack’s
very
own site

 

This is Bavaria, Germany!
You will find links to Perl,
bootdisks for system administration,
a lot of Linux,
Windows administration
and some personal belongings.
Perl Oneliners

See "perldoc perlrun" for more information about commandline-parameters.

I could never imagine why people are playing perl-golf. Now I am sitting here try to solve a problem in Perl which I could make faster simply by starting a graphics program. But it's more fun to do it on the command line. Just for practising!

Converting files to lowercase

perl -e 'for (glob "*JPG") { rename $_, lc($_) }'

Resizing Imagefiles

  1. find *JPG
  2. resize to y=278 by convert, an Imagemagick command line utility
  3. lowercase filename as result
  4. print out command
  5. delete original file if wanted
perl -MFile::Basename -e ' 
$end="JPG"; 
$size=278; 
$remove=1;
for $f (glob "*$end") { 
  $r = lc basename($f,".$end"); 
  $cmd="convert $f -resize x$size ${r}_${size}.$end";
  print "$cmd\n";
  `$cmd`;
  unlink($f) if $remove;
}'

Only parts from special lines of an apache logfile

Sometimes you need to filter out some information from a logfile which looks like this:

127.0.0.1 - - [17:32:10 blabla] "GET /me?customer=joe HTTP/1.1" 404 209
127.0.0.1 - - [17:32:11 blabla] "GET / HTTP/1.1" 404 209
1.2.3.4 - - [17:34:13 blabla] "GET /me?customer=harry HTTP/1.1" 404 209
...

to get some extracted information only containing lines with the customernames from a living system like this:

127.0.0.1 --> joe
1.2.3.4 --> harry
...

You need to know perl's commandline parameters to write a one-liner:

tail -f tao.log | perl -n -e 'm{^(.+?)\s.+customer=(\w+)}; print "$1 --> $2\n"'

The perl -n-commandline-parameter builds automatically a loop like "while (<>) { your code here }"

Numbering a line from a csv file

This is useful with large csv-files where you have to know a field-number quickly. The original line looks like this:

31000055|Abraham, Cyril|A boat to live, a life to sail|a short story

The output looks liek this

1. 31000055
2. Abraham, Cyril
3. A boat to live, a life to sail
4. a short story

The code is this (head -n 1 means only to grep the first line of a file)

head -n 1 damed.csv | perl -n -e '@x = split /\|/; $i=1; for (@x) { printf "%d. %s\n", $i++, $_ }'

Show fieldnames from a mysql-database

The usual output from mysql looks like this:

sh-2.05b$ echo "show fields from customers" | mysql your_database
Field   Type    Null    Key     Default Extra
customer        varchar(50)     NO      UNI
street    varchar(40)   YES             NULL

So we have to separate the first word in the line and print it out

echo "show fields from customers" | \
mysql customercare | \
perl -e 'while (<>) { $i++; next if $i==1; @parts = split /\s+/, $_; print "$parts[0]\n" }' 

customer
street

Number of accesses of PCs from squid-logfile

Input looks like this

x y this
x z this
x y that

Script to find out number of lines for every row this|that

# Find out how many accesses are made via squid
# 3rd field is computername
# real input from logfile
# grep papyrus /var/log/squid3/access.log
# test-input with echo:
(echo "x y this"; echo "x z this"; echo "x y that")  \
| perl -e '
# read line by line, grep out pc-name
while(<>) { $pc=(split/\s+/)[2]; $access{$pc}++;}; 
for (keys %access) { push @sorted, [ $_, $access{$_} ] }; 
@sorted = sort { $a->[1] <=> $b->[1] } @sorted; 
for (@sorted) { printf "%s - %d accesses\n", @$_ };'

Output looks like this:

that - 1 accesses
this - 2 accesses
Impressum