Automatic conversion of AWK scripts to perl.
A little awk script that numbers lines:
ls -la /usr | ./linum.plLet's convert this to perl using a2p.
1 : total 240
2 : drwxr-xr-x+ 12 root root 4096 2010-06-21 06:47 .
3 : drwxr-xr-x 22 root root 4096 2011-05-11 12:06 ..
4 : drwxr-xr-x+ 2 root root 69632 2011-05-24 14:07 bin
5 : drwxr-xr-x+ 2 root root 4096 2010-05-05 11:33 games
6 : drwxr-xr-x+ 5 root root 4096 2010-06-21 06:47 i586-suse-linux
7 : drwxr-xr-x+ 52 root root 4096 2011-05-13 04:17 include
8 : drwxr-xr-x+ 156 root root 69632 2011-05-24 14:07 lib
9 : drwxr-xr-x+ 10 root root 4096 2010-06-21 06:47 local
10 : drwxr-xr-x+ 2 root root 12288 2011-05-24 14:07 sbin
11 : drwxr-xr-x+ 213 root root 4096 2011-05-23 13:19 share
echo '{ printf("%5d : %s\n", NR, $0) }' | a2p > linum.plLet's try it out:
chmod +x linum.pl
ls -la /usr | ./linum.pl
1 : total 240
2 : drwxr-xr-x+ 12 root root 4096 2010-06-21 06:47 .
3 : drwxr-xr-x 22 root root 4096 2011-05-11 12:06 ..
4 : drwxr-xr-x+ 2 root root 69632 2011-05-24 14:07 bin
5 : drwxr-xr-x+ 2 root root 4096 2010-05-05 11:33 games
6 : drwxr-xr-x+ 5 root root 4096 2010-06-21 06:47 i586-suse-linux
7 : drwxr-xr-x+ 52 root root 4096 2011-05-13 04:17 include
8 : drwxr-xr-x+ 156 root root 69632 2011-05-24 14:07 lib
9 : drwxr-xr-x+ 10 root root 4096 2010-06-21 06:47 local
10 : drwxr-xr-x+ 2 root root 12288 2011-05-24 14:07 sbin
11 : drwxr-xr-x+ 213 root root 4096 2011-05-23 13:19 share
The code block looks pretty good:
while (<>) {
chomp; # strip record separator
printf "%5d : %s\n", $., $_;
}
3 comments:
holy cow, this is really cool. thanks alot..
Good article...Thanks for sharing it.
Unix Interview Questions
On the same note as a2p, there's:
- s2p to conver sed to perl
- find2perl to convert find
- sh2p to conver bourne to perl
Post a Comment