Recently, I saw a interesting post on StackOverflow, which compares the difference among sed, AWK, Perl, Python. I decided to learn the “old tricks”. Then, I tried some simple tasks in AWK, Perl, Ruby, and Python. Why Ruby among these? Although Ruby is not a standard tool in a tranditional Unix toolbox, Ruby becomes more popular among these years and Ruby is sometimes viewed as the unadvertised successor of Perl 5.
If you want to try these tasks, please go to UCI Machine Learning Repository and download one dataset. I tried Wine dataset, but any dataset should be fine. The tasks here are just for demo purpose. The better way to do serious statistical analysis with these datasets may be opening a R session and feed some R commands.
Let’s take a glimpse to Wine dataset:
As you see, the dataset is in CSV format. There are total 14 variables here. The first variable is the class of some wines; other variables are some properties of wines. Since we didn’t do serious analysis here, just ignore these facts and concentrate on numbers.
Say I want to change the first column value to its square, try this simple AWK one-liner:
The first column is changed from 3 to 9. It’s intuitive to do field computations in AWK, since AWK has built-in field manipulations.
Let’s try the same task with a Perl one-liner:
The same result as expected, but a little longer.
This time, a Ruby one-liner:
Ruby code here looks like its Perl cousin. However, there are one difference: you have to do explict type conversion.
Since Python has no direct support for this kind of one-line program, we skip Python example in this round.
Let try a more complex task: we have three classes of wines in our dataset and we want the average numbers of these properties of each class.
Let’s start with an AWK script:
We can see that the code of AWK is simple and intuitive. A trick here is that there is no support to multi-dimensional array in original AWK and the index of AWK array is not integer but string; therefore, the name “associative array” for AWK array. We put a comma between our index numbers to mimic a 2-D array. If you want a real multi-dimensional array in AWK, consider GAWK.
Then, the same task with a Perl script:
You can see that the equalavent code is more compact in Perl than in AWK, because Perl support post statement if/while/for. Besides, Perl supports multiple dimension array natively.
Let’s try a Ruby one:
The Ruby script and the Perl one share some similarities. However, we can find some differences between them. First, you cannot use auto-vivification like you did in Perl or AWK script. Second, like we mentioned before, you have to do type conversion. Third, you use more object-oriented features in Ruby than in Perl or AWK.
Finally, we try a Python solution. Python has no direct support for line-oriented program as other three language; Python code does things in a more “formal” ways:
As you see, you have to explictly split line in text file into array. Besides, you have to do type conversion as well, though in a non-OO way. No auto-vivification, of course.
Let’s do a simple statistics for our four scripts:
In our statistics, Perl code tends to be compact and frugal; means time-saving. AWK also produces simple code, though less compact one. Ruby also produces tight code, but you have to think in OO way. Python has no direct support for one-line program or line-oriented programming. Besides, Python code tends to be longer. However, Python code is more readable.
Back to our original question, is AWK still learning even if we have Perl or Ruby? If you just want to learn one tool for text processing, Perl is the best choice. However, AWK is simple and intuitive in some situations; some AWK program is even simpler than Perl one. For simple tasks,
sed and AWK are usually handy. Although Ruby absorbs many features of Perl, you still have to re-learn some Ruby features and think in Ruby way. Python code is succinct and concise, but less support for quick-and-dirty text processing tasks. I think these old tools like AWK is still worth learning, but for long and complex tasks, Perl/Python/Ruby may be more proper.
These demos are just personal tastes for AWK, Perl, Python, and Ruby. They don’t present the best practices of these languages. Every utility and language is born for some reasons. Keep open mind and enjoy your tools.