• About

    Swaroop C H is 27 years of age. He graduated in B.E. (Computer Science) from PESIT, Bangalore, India. He has previously worked at Yahoo! and Adobe.


    Email: swaroop (at) swaroopch.com

    Read more about him

  • Subscription

    If you want to know when new stories and articles appear on this website, you can subscribe to the RSS feed or have them emailed to you.

  • 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