Home » Debian, Linux, Quick Tip, Shell

Quick Tip: Simple parse a CSV file with awk

1 October 2009 No Comment

Writing shell scripts is not an easy thing if you don’t know the power of awk, sed, grep. Today I will show you how to parse a simple csv file and extract only the needed information, with awk.

For example we have a file with the following content:

red, blue, 123, xxx
red, orange, 321, yyy
blue, orange, 312, zzz

To extract only column 1 and 3 use:

$ awk -F ',' '{print $1", "$3}' /path/to/file
red, 123
red, 321
blue, 312

With -F you can specify the field separator, so you can parse any file what contain separators.

BlinkList Diigo Digg Facebook Furl Google Mister Wong Newsvine Reddit StumbleUpon Simpy Technorati Yahoo DZone

Hide Bookmarks

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.