post

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!

Advertisements

Your comments?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s