The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Unix shell script to parse the contents of comma-separated file KrishnaSaran Shell Programming and Scripting 11 06-20-2008 05:43 AM
Remove whitespaces between comma separated fields from file nitinbjoshi UNIX for Dummies Questions & Answers 2 06-14-2008 08:14 AM
Re-usable function to parse csv files with different number of fields jy2k7ca Shell Programming and Scripting 2 03-21-2008 09:53 AM
Append tabs at the end of each line in NAWK -- varying fields madhunk Shell Programming and Scripting 6 07-12-2006 06:20 PM
Splitting comma separated values into an array tmarikle Shell Programming and Scripting 3 06-24-2005 05:50 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-12-2008
2reperry 2reperry is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 15
Parse apart strings of comma separated data with varying number of fields

I have a situation where I am reading a text file line-by-line. Those lines of data contain comma separated fields of data. However, each line can vary in the number of fields it can contain. What I need to do is parse apart each line and write each field of data found (left to right) into a file.

Example Lines of data
---------------------
abc,123,def,456,ghi,789
123,def,456
def,456,ghi,789,jkl
abc
def,456,ghi,789

After the first read the variable $LINE would equal "abc,123,def,456,ghi,789".
Then the fields of data should be parsed and written to FILE.TXT and should look like this :
abc
123
def
456
ghi
789

The second line is read and the variable $LINE would equal "123,def,456" and it's fields of data would be parsed and written to FILE.TXT and would look like so :
123
def
456

and so on for the remaining lines that are read into $LINE.

I have no problems with reading in the lines of data and placing each line into the $LINE variable. I need assistance with how to parse apart each line as it's read into $LINE and writing the fields to a file one-by-one from left to right. I'm pretty sure awk would probably be the best method but I'm open to suggestions on any more efficient methods/commands.

Can anyone help?
  #2 (permalink)  
Old 09-12-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Something like this?

Code:
tr ',' '\n' < file > FILE.TXT
Regards
  #3 (permalink)  
Old 09-12-2008
dennis.jacob dennis.jacob is offline Forum Advisor  
dj -------
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 560
Try:

Code:
tr ',' '\n' < filename
or,

Code:
sed 's/,/\n/g' filename
  #4 (permalink)  
Old 09-12-2008
n1djs n1djs is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 12
sed -e 's/\,/^M/g' filename >newfilename
  #5 (permalink)  
Old 09-12-2008
dennis.jacob dennis.jacob is offline Forum Advisor  
dj -------
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 560
Quote:
Originally Posted by n1djs View Post
sed -e 's/\,/^M/g' filename >newfilename
this will not work. ^M is the DOS LF character.
  #6 (permalink)  
Old 09-12-2008
2reperry 2reperry is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 15
Sorry, but, maybe I wasn't clear enough in my original thread. tr won't work for me because I need to do processing on each string after it's been parsed and before reading of the next record. If I'm reading this right the tr command will read input from 'file' and write the results to FILE.TXT. This will not allow me to do other processing after parsing the line and writing to FILE.TXT. After each read and subsequent parsing of $LINE I am going to do other processing with the parsed data before reading the next record and parsing it. And so on....

Process Flow/Steps/Loop
------------------------
1) Read a record in $LINE (which I already handle via a while loop)
2) Parse $LINE and output fields to FILE.TXT (which is what I need help with)
3) Then I will do other processing using the field data written out to FILE.TXT (which I will handle)
4) Step 1, until end of file.

Step 2 is the only thing I need assistance with. I need to take what's in $LINE, no matter how many comma separated elements there may be, parse them out and write them, one at-a-time, to FILE.TXT. Once all elements have been written out for $LINE I will then use that data for other processing. Then I will read the next record into $LINE. And so on.
  #7 (permalink)  
Old 09-12-2008
2reperry 2reperry is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 15
I think I've got this one figured out thanks to your valuable input. Much thanks!

What I did, based on your input :

I read each record into $LINE and then do the following :

echo "$LINE" | tr ',' '\n' > FILE.TXT

Then all I do is read the FILE.TXT file for each of the individual elements/field data.

Thanks again for pointing me in the right direction.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:35 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0