LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-24-2009, 09:50 AM   #1
indiancosmonaut
Member
 
Registered: Feb 2007
Posts: 65

Rep: Reputation: 15
A data parsing problem - AWK


Hi,

I am having some problems in parsing the following data.

Code:
Tom
AGE:25
EDINBURGH

Dick
AGE:24
AMSTERDAM

Harry
AGE:23
BRUSSELS
I am writing a code to generate the following CSV file.

Code:
"AGE:25 EDINBURGH","AGE:24 AMSTERDAM","AGE:23 BRUSSELS"
I started writing the code but I am not able to determine how to get this data because of the following contraints...

a) The data blocks can be in any order


Code -

Code:
function init_array()
{
        for (del in feed)
                delete feed[del];

        i=0;

        _Tom_ = 0;
        _Dick_ = 0;
        _Harry_ = 0;
}

BEGIN { i=0; }

{ record = $0; }

/^$/ { next; }

/Tom/ {
                print "Now reading :Tom:";
                init_array();
                _Tom_ = 1;
                next;
        }

/Dick/ {
                print "Now reading :Dick:";
                init_array();
                _Dick_ = 1;
                next;
        }

/Harry/ {
                print "Now reading :Harry:";
                init_array();
                _Harry_ = 1;
                next;
}

{

        print NR" : "record;
        feed[i]=record;
        i++;
}


function one()
{
        for (j in feed)
                 ret=ret"\n"feed[j];

        return (sprintf("%s",ret));
}

if ( _Tom_ == 1)
Tom=one();
..... (But this doesn't seem right)


END { print Tom,",", Dick,",", Harry; };
Its incomplete, but I am looking for some help.

Kind regards,

indiancosmonaut

Last edited by indiancosmonaut; 02-24-2009 at 09:51 AM.
 
Old 02-24-2009, 10:34 AM   #2
amani
Senior Member
 
Registered: Jul 2006
Location: Kolkata, India
Distribution: 64-bit GNU/Linux, Kubuntu64, Fedora QA, Slackware,
Posts: 2,758

Rep: Reputation: Disabled
Basically you need to read the original as a csv take the string in the 3x th line and concatenate it to the string in the 3x-1 th line and form a new csv with the new 3x-1 th lines. You seem to want col names only with 0 rows (is easy). Takes about 1 or 2 lines in R

or just delete the 3x th lines
 
  


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
parsing text using sed/awk or similar??? freeindy Programming 5 07-24-2008 04:04 AM
Parsing using awk tungaw2001 Programming 2 03-27-2007 05:45 AM
awk question - parsing xml file epoo Programming 7 01-24-2007 02:13 PM
Sed or Awk question, looking for parsing help rwartell Linux - Software 2 05-17-2006 11:59 PM
Sed or Awk question, looking for parsing help rwartell Programming 1 05-17-2006 04:42 PM


All times are GMT -5. The time now is 07:15 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration