Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 04-25-2012, 06:22 AM   #1
LQ Newbie
Registered: Apr 2012
Posts: 1

Rep: Reputation: Disabled
Parse a CSV file output to text file

I have a 12 columns and 50 rows csv file as follows:

usa city,losangels,sanfran,denver,chicago,boston,newyork,miami
losangels,---, 1500,134,1470,123,456,234
They are USA cities with distances to each other.
I want to parse it so it looks like this and it will go row by row, maybe output it to text file

totaldistance["losangels"]tonextcity("losangels", ---));
totaldistance["losangels"]tonextcity("sanfran", 1500));
Old 04-25-2012, 06:41 AM   #2
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 10,082

Rep: Reputation: 2982Reputation: 2982Reputation: 2982Reputation: 2982Reputation: 2982Reputation: 2982Reputation: 2982Reputation: 2982Reputation: 2982Reputation: 2982Reputation: 2982
looks like a job for awk or perl (maybe sed). What have you tried? What do you prefer?
Old 04-25-2012, 06:49 AM   #3
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 592

Rep: Reputation: 140Reputation: 140
Looks like a simple bash script
pseudo code
read first line to string or array
use a loop to traverse all the cities
  read the next line
  use another loop
    print the distances from the line read.
Old 04-25-2012, 09:45 AM   #4
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
An awk solution:
awk -F, '
NR == 1 {
  for ( i = 1; i <= NF; i++ )
    cities[i] = $i      

NR > 1 {
  for ( i = 2; i <= NF; i++ )
    printf "totaldistance[\"%s\"]tonextcity(\"%s\", %s));\n", $1, cities[i],$i
}' file


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Pulling text from a text file and append to CSV fishy Linux - Newbie 5 03-02-2012 08:32 AM
[SOLVED] parse csv file with millions of records conflicker Programming 8 02-20-2012 08:12 AM
[SOLVED] Redirecting terminal output to text file leads to corrupted text file mariovrn Linux - Newbie 5 11-30-2011 08:21 AM
parse input text file and generate output TsanChung Programming 5 07-27-2008 11:23 PM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 03:36 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:35 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration