3f4b Shell, Awk, Perl of Python?
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Shell, Awk, Perl of Python?

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].

(written 2012-06-14, updated 2012-06-16)

 
Associated topics are indexed under
Q102 - Choosing your language
  [3785] Programming languages - what are the differences between them? - (2012-06-27)
  [3619] Ruby v Perl - a comparison example - (2012-02-21)
  [3558] Python or Lua - which should I use / learn? - (2011-12-21)
  [3169] Rekeying a table - comparison in #Ruby #Perl and #Python - (2011-02-14)
  [2866] Ruby - how does it compare and where is it the right language? - (2010-07-11)
  [2700] The same very simple program in many different programming languages - (2010-03-31)
  [2536] All the Cs ... and Java too - (2009-12-13)
  [2535] When should I use Java, Perl, PHP, or Python? - (2009-12-13)
  [2507] Admission - (2009-11-19)
  [2048] Learning to program in PHP, Python, Java or Lua ... - (2009-02-19)
  [2001] I have not programmed before, and need to learn - (2009-01-19)
  [1990] Speaking all the languages - (2009-01-12)
  [76] Learning to program in - (2004-10-07)

P669 - Perl - Data Munging
  [3707] Converting codons via Amino Acids to Proteins in Perl - (2012-04-25)
  [3335] Practical Extraction and Reporting - (2011-06-26)
  [2702] First and last match with Regular Expressions - (2010-04-02)
  [2129] Nothing beats Perl to solve a data manipulation requirement quickly - (2009-04-14)
  [1947] Perl substitute - the e modifier - (2008-12-16)
  [1509] Extracting information from a file of records - (2008-01-16)
  [1316] Filtering and altering Perl lists with grep and map - (2007-08-23)
  [597] Storing a regular expression in a perl variable - (2006-02-09)

Y110 - Python - File Handling
  [3465] How can I do an FTP transfer in Python? - (2011-10-05)
  [3442] A demonstration of how many Python facilities work together - (2011-09-16)
  [3083] Python - fresh examples from recent courses - (2010-12-11)
  [2870] Old prices - what would the equivalent price have been in 1966? - (2010-07-14)
  [2282] Checking robots.txt from Python - (2009-07-12)
  [2011] Conversion of OSI grid references to Eastings and Northings - (2009-01-28)
  [1442] Reading a file multiple times - file pointers - (2007-11-23)
  [183] The elegance of Python - (2005-01-19)
  [114] Relative or absolute milkman - (2004-11-10)

A166 - Web Application Deployment - Linux Utilities
  [3902] Shell - Grep - Sed - Awk - Perl - Python - which to use when? - (2012-10-22)
  [3446] Awk v Perl - (2011-09-18)
  [2638] Finding what has changed - Linux / Unix - (2010-02-17)
  [2484] Finding text and what surrounds it - contextual grep - (2009-10-30)
  [2320] Helping new arrivals find out about source code examples - (2009-08-03)
  [2145] Using the internet to remotely check for power failure at home (PHP) - (2009-04-29)
  [1690] Conversion of c/r line ends to l/f line ends - (2008-06-28)
  [1366] awk - a powerful data extraction and manipulation tool - (2007-09-25)
  [1361] Korn shell course - (2007-09-22)
  [71] Comparators in Linux and Unix - (2004-10-03)
  [63] Almost like old times - (2004-09-26)


587c
Back to
Spike solutions and refactoring - a Python example
Previous and next
or
Horse's mouth home
Forward to
Christmas in June? Melksham hotel bookings and Santa train
Some other Articles
Melksham Chamber of Commerce - looking to our future shape. Pivotal meeting next Tuesday
How well do you know Melksham?
Python timing - when to use a list, and when to use a generator
Christmas in June? Melksham hotel bookings and Santa train
Shell, Awk, Perl of Python?
Spike solutions and refactoring - a Python example
Learning to program - the if statement. Python.
Melksham - placed 2254 out of 2255. What can be done about it?
Why you should use objects even for short data manipulation programs in Ruby
The five oldest blogs and the horses mouth
4090 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2013: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3764_She ... thon-.html • PAGE BUILT: Sat Feb 23 12:39:13 2013 • BUILD SYSTEM: wizard
0