Main Content

Shell, Awk, Perl of Python?

Archive - Originally posted on "The Horse's Mouth" - 2012-06-14 18:45:55 - Graham Ellis

Very frequently, IT system users find they've got a data file in one format and they need to filter it and transform it. On Linux and Unix systems, utilities such as grep and cut were the tradtions, perhaps with some awk.

Perl provided a single-program replacement for shell scripts that bolted together the Unix utilites, and it can still do a great job. But Perl code can often be very hard to modify and extend later on, and very often similar reformatting jobs will recur. And that's where, these days, I would recommend another language - perhaps Pyton rather than Perl.

From this week's course - a short Python example that reads a space delimited data file, checks whether the number in the third field is greater than 40, and if it is outputs the record tab delimited:

  for line in open("melksham.txt"):
    fields = line.split()
    if int(fields[2]) > 40:
      newline = "\t".join(fields)
      print newline


Complete file [here].

In Perl, it's rather shorter but harder to follow:

  FH = open("melksham.txt);
  while () {
    @f = split(/\s+/);
    print (join("\t",@f),"\n") if ($f[2] > 40);
    }


See our full course schedule - including both Python and Perl courses - [here].