Results 1 to 6 of 6
Hello ,
I am using RHEL 5.0 and I have some trouble with a CSV file.
What I need to do is to grab all the elements from the 3rd ...
- 08-06-2010 #1Just Joined!
- Join Date
- Aug 2010
- Posts
- 5
How to create a script that parses a CSV file
Hello ,
I am using RHEL 5.0 and I have some trouble with a CSV file.
What I need to do is to grab all the elements from the 3rd column and place them each on a new line in a different (new) file.
For example, if the CSV file is:
1,2,3,4
a,b,c,d
some,may,write,pool
then the output file should be:
3
c
write
I tried to threat each line as a string and grab the 3rd element, but for some reason it fail.
Can you kindly help me out with an example?
Thanks.
- 08-07-2010 #2Linux User
- Join Date
- Jan 2007
- Location
- cleveland
- Posts
- 452
welcome to the forum
if the CVS file is called "file" then something like this should work:
cut -d , -f 3 <filethe sun is new every day (heraclitus)
- 08-07-2010 #3Linux Newbie
- Join Date
- Apr 2010
- Location
- Novosibirsk, Russia
- Posts
- 136
Within a script, you can use regular expressions, or 'split' function.
Using split:
regexp:Code:my @words = split ",", $csv_line; my $needed_word = words[2];
Code:$csv_line =~ /^[^,]*,[^,]*,([^,]*),/; $needed_word = $1;
- 08-07-2010 #4
In some cases, you can parse CSV files simply by using tools such as cut. However, these break down as soon as you have fields with embedded commas or quotes. For those cases you are probably best to use some kind of tool built specifically for the purpose of parsing CSV data. You haven't said what programming language you want to use, but my weapon of choice would be Perl, with one of the numerous packages crafted for the purpose, and available from CPAN.
--- rod.Stuff happens. Then stays happened.
- 08-10-2010 #5Just Joined!
- Join Date
- Jul 2010
- Posts
- 53
awk -F, '{print $3}'
- 08-13-2010 #6Just Joined!
- Join Date
- Aug 2010
- Posts
- 5
Thanks for the advice. I managed to collect data from the CSV file using the tips you provided.


Reply With Quote