Scripting one-liners

less than 1 minute read

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!