Solved

Another awk question

Posted on 2009-07-08
5
159 Views
Last Modified: 2012-05-07
Hello,

I have a data file:

1,1,a,b
1,1,b,x
1,1,c,1
1,2,a,2
1,2,b,3
1,2,c,4
2,1,a,6
2,1,b,g
2,1,c,h
2,2,a,2
2,2,b,p
2,2,c,4

The first column is the key, second column is field ID, third is tag and forth is value.
I want to get it into this format

key {fieldid {tag value tag value...} fieldid {tag value ...}}
key {fieldid {tag value tag value...} fieldid {tag value ...}}

So in example above I get the following output:

1 {1 {a b b x  c 1} 2 {a 2 b 3 c 4}}
2 {1 {a 6 b g c h } 2 {a 2 b 2 c 4}}

Is this possible using awk? If so how?
0
Comment
Question by:chuntse
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 

Author Comment

by:chuntse
ID: 24805456
The key, fieldid, tag and values will be varying length
0
 
LVL 84

Expert Comment

by:ozo
ID: 24806871
how will they vary, and what would the output be in that case?
0
 

Author Comment

by:chuntse
ID: 24807367
An altered example:

1,1,a,b123
1,1,b1,x23
1,1,cxq,12211
1,21,a1,2
1,21,b21,3
1,21,ca1f,400

becomes

1 {1 {a b123 b1 x23  cxq 1} 21 {a1 2 b21 3 ca1f 400}}
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 24809089
awk -F, '{if(  f1 != $1 ){  if( f ){ print( f "}}"); }  f = $1 " {" $2 " {" $3 " " $4; f2=$2} if( f2 != $2 ){ f=f "} "$2 " {";}  f1 = $1; f2 = $2; f = f " " $3 " " $4; } END{  print( f "}}") }'
0
 

Author Closing Comment

by:chuntse
ID: 31601191
Great thanks.

Some values contain spaces and I need to enclose in braces but if I can't work if out using your example then I'll post another question.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question