Tuesday, October 22, 2019

Parsing Text Files With Perl

Parsing Text Files With Perl Parsing text files is one of the reasons Perl makes a great data mining and scripting tool. As youll see below, Perl can be used to basically reformat a group of text. If you look down at the first chunk of text and then the last part at the bottom of the page, you can see that the code in the middle is what transforms the first set into the second. How to Parse Text Files As an example, lets build a little program that opens up a tab separated data file, and parses the columns into something we can use. Say, as an example, that your boss hands you a file with a list of names, emails, and phone numbers, and wants you to read the file and do something with the information, like put it into a database or just print it out in a nicely formatted report. The files columns are separated with the TAB character and would look something like this: Larry larryexample.com 111-1111 Curly curlyexample.com 222-2222 Moe moeexample.com 333-3333 Heres the full listing well be working with: #!/usr/bin/perl open (FILE, data.txt); while (FILE) { chomp; ($name, $email, $phone) split(\t); print Name: $name\n; print Email: $email\n; print Phone: $phone\n; print -\n; } close (FILE); exit; Note:Â  This pulls some code from the tutorial on how to read and write files in Perl. What it does first is open a file called data.txt (that should reside in the same directory as the Perl script). Then, it reads the file into the catchall variable $_ line by line. In this case, the $_ is implied and not actually used in the code. After reading in a line, any whitespace is chomped off the end of it. Then, the split function is used to break the line on the tab character. In this case, the tab is represented by the code \t. To the left of the splits sign, youll see that Im assigning a group of three different variables. These represent one for each column of the line. Finally, each variable that has been split from the files line is printed separately so that you can see how to access each columns data individually. The output of the script should look something like this: Name: Larry Email: larryexample.com Phone: 111-1111 - Name: Curly Email: curlyexample.com Phone: 222-2222 - Name: Moe Email: moeexample.com Phone: 333-3333 - Although in this example were just printing out the data, it would be trivially easy to store that same information parsed from a TSV or CSV file, in a full-fledged database.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.