• About

    Swaroop C H is 27 years of age. He currently works at Infibeam, an ecommerce company focused on India. He has previously worked at Yahoo!, Adobe and his own startup.


    Read more about him


    Email: swaroop (at) swaroopch.com

  • Subscription

    If you want to know when new stories and articles appear on this website, you can receive them via:

  • Explore

  • Want me to write about something?

  • I'm a Wannabe Hacker

    The Glider: A Universal Hacker Emblem

Scripting one-liners

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!

Please rate whether you liked this article:

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Comments are closed.

Additional comments powered by BackType