Working with CSV Files from the Command Line
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Dart: a New Web Programming Experience
- Download the Free Red Hat White Paper "Using an Open Source Framework to Catch the Bad Guy"
- The Secret Password Is...
- Linux from the Beginning
- RSS Feeds
- myip
1 hour 21 min ago - Keeping track of IP address
3 hours 12 min ago - Roll your own dynamic dns
8 hours 25 min ago - Please correct the URL for Salt Stack's web site
11 hours 36 min ago - Android is Linux -- why no better inter-operation
13 hours 52 min ago - Connecting Android device to desktop Linux via USB
14 hours 20 min ago - Find new cell phone and tablet pc
15 hours 18 min ago - Epistle
16 hours 47 min ago - Automatically updating Guest Additions
17 hours 56 min ago - I like your topic on android
18 hours 42 min ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
Thanks a lot! Such a concise
Thanks a lot! Such a concise and "let's get started without talking log" video helps me a lot to learn efficiently in the CLI!
I recommend the csv module
I recommend the csv module in python.
http://docs.python.org/library/csv.html
It is really easy to use and handles fields with embedded newlines, quotes, commas with flying colors!
Give it a shot some time, you won't be disappointed :).
-TH
Big caveat
Most CSV files I get these days have quoted strings containing commas inside the quoted field between the separators.
You'd better be very sure of your CSV contents if you're going to use cut like this.
It'll fail on this CSV file:
1,Cat food,55.69,05/04/2009
2,Power bill,149.75,03/04/2009
3,Rent,"1,350.00",01/04/2009
Use Grep
My original thinking was to use grep:
to extract the email but I decided on cut to simplify the "look" of it a bit for the video.
Of course your sample doesn't include any emails so that wouldn't work... so you'd have to adapt/change it to extract the right field.
A perhaps more robust method is to convert the CSV file to use tabs rather than commas. With tabs cut should work most of the time. Still might be that you'd have quoted tabs, but unless your CSV came from a database dump that seems unlikely.
Mitch Frazier is an Associate Editor for Linux Journal.
How will grep help us parse?
That grep will identify lines that are CSV, but won't parse out the values. And yes, I deal with tons of huge CSV files that go in and out of databases, so it's important that I get my scripts right.
The only thing I've found that comes close to what I need is http://perlmeme.org/tutorials/parsing_csv.html -- so I'm in the process of rewriting that in to something bash can invoke from inside shell scripts.
Forgot an option
Forgot the -o option:
that will show you only the part that matched.
Mitch Frazier is an Associate Editor for Linux Journal.
But that's still not "parsing"
Sure, that'll extract an email address, but it doesn't fit any accepted definition of "parsing," since it assumes that you have prior knowledge of what's in the field and can't properly handle cases of having more than one @ in any line.
You're handling strings of text of which you can predict formatting, but you are certainly not using techniques that are transferrable to the general case of "I need to parse a CSV file."
That's Correct
You're 100% right, neither cut nor grep are generalized solutions for parsing CSV files, but then again I never said they were. As far as I know I never used the word parse, that was your word.
What I meant to say was that grep is both a floor wax and a dessert topping..png)
Mitch Frazier is an Associate Editor for Linux Journal.
No need for uniq
GNU
sorthas a--unique(-u) argument that checks sorts and checks for uniqueness in the same pass. So you can just use "sort -uf" or "sort --ignore-case --unique" skippinguniqaltogether.-Tim
Thanks
Yes it does and that's why I always say one should re-RTFM once in a while to refresh one's memory and check for new features.
.png)
I might also add that one shouldn't jump to the the conclusion that the -u option makes uniq obsolete: uniq has some other useful options that come in handy once in a while. So re-RTFM applies to uniq also.
Mitch Frazier is an Associate Editor for Linux Journal.
Beat me to it
I'm glad somebody mentioned this; I found out about that a couple of months ago. Much better than having to pipe to yet another process :D
--
Regards,
Matthew Cengia