What are the differences between Perl, Python, AWK and sed?

In order of appearance, the languages are sed, awk, perl, python.

The sed program is a stream editor, and is designed to apply the actions from a script to each line (or, more generally, to specified ranges of lines) of the input file or files. Its language is based on ed, the Unix editor, and although it has conditionals and so on, it is hard to work with for complex tasks. You can work minor miracles with it – but at a cost to the hair on your head. However, it is probably the fastest of the programs when attempting tasks within its remit. (It has the least powerful regular expressions of the programs discussed – adequate for many purposes, but certainly not PCRE – Perl-Compatible Regular Expressions)

The awk program (name from the initials of its authors – Aho, Weinberger and Kernighan) is a tool originally for formatting reports. It can be used as a souped up sed; in its more recent versions, it is computationally complete. It uses an interesting idea – the program is based on ‘patterns matched’ and ‘actions taken when the pattern matches’. The patterns are fairly powerful (Extended Regular Expressions). The language for the actions is similar to C. One of the key features of awk is that it splits the input lines into fields automatically.

Perl was written in part as an awk-killer and sed-killer. Two of the programs provided with it are a2p and s2p for converting awk scripts and sed scripts into Perl. Perl is one of the earliest of the next generation of scripting languages (Tcl/Tk can probably claim primacy). It has powerful integrated regular expression handling with a vastly more powerful language. It provides access to almost all system calls, and has the extensibility of the CPAN modules. (Neither awk nor sed is extensible.) One of Perl’s mottos is “TMTOWTDI – There’s more than one way to do it” (pronounced “tim-toady”). Perl has ‘objects’, but it is more of an add-on than a fundamental part of the language.

Python was written last, and probably in part as a reaction to Perl. It has some interesting syntactic ideas (indenting to indicate levels – no braces or equivalents). It is more fundamentally object-oriented than Perl; it is just as extensible as Perl.

OK – when to use each?

sed – when you need to do simple text transforms on files.
awk – when you only need simple formatting and summarization or transformation of data.
perl – for almost any task, but especially when the task needs complex regular expressions.
python – for the same tasks that you could use Perl for.
I’m not aware of anything that Perl can do that Python can’t, nor vice versa. The choice between the two would depend on other factors. I learned Perl before there was a Python, so I tend to use it. Python has less accreted syntax and is generally somewhat simpler to learn. Perl 6, when it becomes available, will be a fascinating development.

Perl or Python are far better than any version of awk or sed when you have very complex input/output scenarios. The more complex the problem is, the better off you are using python, from a maintenance and readability standpoint. Note, however, that a good programmer can write readable code in any language, and a bad programmer can write unmaintainable crap in any useful language, so the choice of perl or python can safely be left to the preferences of the programmer if said programmer is skilled and clever.

Perl or Python are far better than any version of awk or sed when you have very complex input/output scenarios. The more complex the problem is, the better off you are using python, from a maintenance and readability standpoint. Note, however, that a good programmer can write readable code in any language, and a bad programmer can write unmaintainable crap in any useful language, so the choice of perl or python can safely be left to the preferences of the programmer if said programmer is skilled and clever.

Perl and Python are fully fledged, general purpose programming languages. Perl has its roots in text processing and has a number of awk-like constructs (there is even an awk-to-perl script floating around on the net). There are many differences between Perl and Python, your best bet is probably to read the summaries of both languages on something like Wikipedia to get a good grasp on what they are.

There is plenty of difference between them and between all the other
scripting languages. Look beyond the notion of “scripting” and look at
the programming language and the paradigm it supports.

Personally, I don’t see Python as much of a scripting language in the
sense that Perl is, where Perl is the “king of the quick hack”…
powerful, easy to throw together a script to get a job done in a hurry.
Not so easy (but undeniably possible) to keep a disciplined, careful
approach to writing a sizable application which will be maintained over
time.

Python, on the other hand, feels more to me like a solid, disciplined
object-oriented (dare I say “real”) programming language which happens
to be high-level, dynamic and compiled to bytecode at runtime (much as
Java is). It takes a little more work to make a program in Python than
Perl, but the code tends to be more solid and easier to maintain.

Those are gross generalizations, and depend on programmer skill, but the
languages approach programming from such different angles, and encourage
such different mindsets that it’s worth considering as a difference.

Each language has separate strengths. Python is less ambiguous about
what expressions mean (Perl tries hard to “guess” at an appropriate way
to interpret almost anything to do *something* while Python rejects
things that are not clear and explicit as to your intent). Python is
*far* stronger at OOP support than Perl. Perl has more convenient
short-cuts for many things, but I’ve found the completeness of the
Python standard lib and the flexibility of the methods and functions
inside it to ultimately give me more power even if I can’t do it in
three characters.

The list could go on and on, but it really comes down to what you want
to accomplish. I’d use Perl for a lot of “scripting” kinds of needs,
one-off scripts, simple data reformatting, process automation, but would
prefer Python for creating more complex applications. Likewise, TCL and
Ruby have their own particular domains they do well within.

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s

%d bloggers like this: