Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting numbers

Status
Not open for further replies.

userand

Technical User
Apr 12, 2011
28
CH
Hi all,

Can anyone help me to convert the following, which actually what I would like to get from the previous lines:

I have the file

1 6 8
1 1 9
2 6 14
2 1 14
4 6 5

I would like to get

1 9 0 0 0 0 8
2 14 0 0 0 0 14
4 0 0 0 0 0 5

So the first column is just a row number, the 2nd is the position of the 3rd column value; so 1 9 0 0 0 0 8 is the first row and there are two positions: 9 at position 1, and the other one is in position 6 with value 8.

Could you suggest some code or code part to do this.
 
Hi

Code:
awk 'p[teal]&&[/teal]p[teal]!=[/teal][navy]$1[/navy][teal]{[/teal]w[teal]()[/teal][teal]}{[/teal]p[teal]=[/teal][navy]$1[/navy][teal];[/teal][b]if[/b][teal]([/teal]m[teal]<[/teal][navy]$2[/navy][teal])[/teal]m[teal]=[/teal][navy]$2[/navy][teal];[/teal]a[teal][[/teal][navy]$2[/navy][teal]]=[/teal][navy]$3[/navy][teal]}[/teal]END[teal]{[/teal]w[teal]()[/teal][teal]}[/teal][COLOR=darkgoldenrod]function[/color] w[teal]()[/teal][teal]{[/teal][COLOR=chocolate]printf[/color][green][i]"%d"[/i][/green][teal],[/teal]p[teal];[/teal][b]for[/b][teal]([/teal]i[teal]=[/teal]1[teal];[/teal]i[teal]<=[/teal]m[teal];[/teal]i[teal]++)[/teal][teal]{[/teal][COLOR=chocolate]printf[/color][green][i]" %d"[/i][/green][teal],[/teal]a[teal][[/teal]i[teal]];[/teal][b]delete[/b] a[teal][[/teal]i[teal]][/teal][teal]}[/teal]m[teal]=[/teal]0[teal];[/teal][COLOR=chocolate]print[/color][green][i]""[/i][/green][teal]}[/teal]' /input/file
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].


Feherke.
 
Hi,

that's nice. Just one more simple question: how can I modify the code if I want to get

1 9 0 0 0 0 8 0 0 0 0 0 0 0 0 0 0

instead of

1 9 0 0 0 0 8

So the number of columns should be 16 in any cases; and for example the second column of

1 6 8
1 1 9
2 6 14
2 1 14
4 6 5

just indicating the position of the 3rd column in a 16 digit sequence. So the number of columns are fixed by default.

I just want to get:

1 9 0 0 0 0 8 0 0 0 0 0 0 0 0 0 0
2 14 0 0 0 0 14 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0

Thanks!
 
Sorry, my last question makes no sense. It is trivial.

But what i would like to ask if there is

1 6 8
1 1 9
2 6 14
2 1 14
4 6 5

How can I get this

6 8
1 1 9
2 6 14
2 1 14
3 0 0
4 6 5

or rather


1 9 0 0 0 0 8 0 0 0 0 0 0 0 0 0 0
2 14 0 0 0 0 14 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0

So the first column should be growing by one unit.

Thanks!
 
Hi

Code:
awk 'BEGIN[teal]{[/teal]p[teal]=[/teal]1[teal]}[/teal]p[teal]!=[/teal][navy]$1[/navy][teal]{[/teal][b]for[/b][teal](;[/teal]p[teal]<[/teal][navy]$1[/navy][teal];[/teal]p[teal]++)[/teal]w[teal]()[/teal][teal]}{[/teal]p[teal]=[/teal][navy]$1[/navy][teal];[/teal]a[teal][[/teal][navy]$2[/navy][teal]]=[/teal][navy]$3[/navy][teal]}[/teal]END[teal]{[/teal]w[teal]()[/teal][teal]}[/teal][COLOR=darkgoldenrod]function[/color] w[teal]()[/teal][teal]{[/teal][COLOR=chocolate]printf[/color][green][i]"%d"[/i][/green][teal],[/teal]p[teal];[/teal][b]for[/b][teal]([/teal]i[teal]=[/teal]1[teal];[/teal]i[teal]<=[/teal]16[teal];[/teal]i[teal]++)[/teal][teal]{[/teal][COLOR=chocolate]printf[/color][green][i]" %d"[/i][/green][teal],[/teal]a[teal][[/teal]i[teal]];[/teal][b]delete[/b] a[teal][[/teal]i[teal]][/teal][teal]}[/teal][COLOR=chocolate]print[/color][green][i]""[/i][/green][teal]}[/teal]' /input/file
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top