![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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? |
|
||||
|
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. |
|
||||
|
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. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|