Swaroop C H

blog books about contact subscribe

Scripting one-liners

14 Oct 2004

I was going through some data files and I had a requirement to get the last character of each line to analyze the files... and so a discussion began between me and Kiran, a fellow yahoo and soon our blr devel group. I contributed a Perl and Python one-liner and Kiran being the Perl guru around here gave 3 more Perl one-liners... after all the Perl motto is "There's more than one way to do it!" We asked others on sed and awk equivalents and soon we had the following list of one-liners:

perl -ne '/(.)$/; print "$1n"' file perl -ne 'print substr($_, -2)' file perl -ne 'print((split//)[-2] , "n")' file perl -F// -ane 'print($F[-2], "n")' file
python -c 'while True: s=raw_input();print s[-1]' sed -n 's/.*(.)$/1/p' file awk -F '' '{ print $NF }'

It was gr8 fun to see this discussion happening so quickly and we found out so many interesting ways to do the same thing. Soon, we had performance comparisons :O .. it seems that the sed way as shown above takes longest time whereas the perl substr way the least!

Feedback

There's no comment box, but please do email me or tweet me your thoughts and criticisms, and I will publish the relevant ones here.