Solved

Another awk question

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

Industry Leaders: 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

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…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

734 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