Solved

Another awk question

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Solaris 10.  Nmap installation fails 2 40
Python 2.7 - French characters 6 47
Register AutoHotkey 12 40
Linux Scripting 3 56
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…
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
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…

758 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

22 Experts available now in Live!

Get 1:1 Help Now