Solved

Another awk question

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
exchange 2007, scripts 15 73
Python 2.7 - French characters 6 59
this script icacle doesn't work on remote computers. please help 11 54
Martian Packets Unix 5 31
It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
The viewer will learn how to dynamically set the form action using jQuery.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now