Solved

Another awk question

Posted on 2009-07-08
5
158 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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

685 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