LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 04-25-2012, 05:22 AM   #1
beto
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:


Code:
usa city,losangels,sanfran,denver,chicago,boston,newyork,miami
losangels,---, 1500,134,1470,123,456,234
sanfran,1500,---,456,780,3456,768,567
.
.
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

Code:
totaldistance["losangels"]tonextcity("losangels", ---));
totaldistance["losangels"]tonextcity("sanfran", 1500));
 
Old 04-25-2012, 05:41 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

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

Rep: Reputation: 141Reputation: 141
Looks like a simple bash script
Code:
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, 08:45 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
An awk solution:
Code:
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
 
  


Reply


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 07:32 AM
[SOLVED] parse csv file with millions of records conflicker Programming 8 02-20-2012 07:12 AM
[SOLVED] Redirecting terminal output to text file leads to corrupted text file mariovrn Linux - Newbie 5 11-30-2011 07:21 AM
parse input text file and generate output TsanChung Programming 5 07-27-2008 10: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 02:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:14 AM.

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