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!

Print out numbers on display

Status
Not open for further replies.

userand

Technical User
Apr 12, 2011
28
CH
Hi,

I am just wondering how can be solved the following problem:

I have the numbers

0 0 0 0 12 0 0 234 1 7 0 0 99 0 9 0 0

Now I would like to convert this to

_ _ _ _ 12 _ _ 234 1 7 _ _ 99 _ 9 _ _

I think I should use a[0]="_"; ...etc. but if I have numbers different from 0's it is a little bit complicated...

Can anyone help me?
 
Hi

What you mentioned there sounds like you are thinking to something like this :
Code:
awk '[teal]{[/teal][b]for[/b][teal]([/teal]i[teal]=[/teal][purple]1[/purple][teal];[/teal]i[teal]<=[/teal][blue]NF[/blue][teal];[/teal]i[teal]++)[/teal][b]if[/b][teal](![/teal][navy]$i[/navy][teal])[/teal][navy]$i[/navy][teal]=[/teal][green][i]"_"[/i][/green][teal]}[/teal][purple]1[/purple]' /input/file
More simple way would be with GNU Awk :
Code:
awk '[teal]{[/teal][COLOR=chocolate]gsub[/color][teal]([/teal][fuchsia]/\<0\>/[/fuchsia][teal],[/teal][green][i]"_"[/i][/green][teal])[/teal][teal]}[/teal][purple]1[/purple]' /input/file
( Sadly the anchoring to word boundaries is not supported in all Awk implementations. )

Tested with [tt]gawk[/tt] and [tt]mawk[/tt].


Feherke.
 
Nice, thanks. Can you split this line

_ _ _ _ 12 _ _ 234 1 7 _ _ 99 _ 9 _


into the following:

_ _ _ _
12 _ _ 234
1 7 _ _
99 _ 9 _

For example I have 16 columns in a row, and I would like to start a new line after every 4th columns.

I did it in this way but not helped so far:

awk '{for(i=1;i<=NF;i++)if(!$i)$i=".";if(i%4)printf("\n");}1'


Could you help me?
 
Hi

Nice try, but wrong theory.
[ul]
[li]the second [tt]if[/tt] you added will be executed only once per record, after the [tt]for[/tt] loop finished ( in that moment i's value will be [tt]NF[/tt]+1 )[/li]
[li]there is absolutely no output in my original [tt]for[/tt], so even if you [tt]printf[/tt] the newlines in the loop, that will not appear between the fields[/li]
[/ul]
Code:
awk '[COLOR=#FF0000]{[/color][b][COLOR=#0000FF]for[/color][/b][COLOR=#990000]([/color]i[COLOR=#990000]=[/color][COLOR=#993399]1[/color][COLOR=#990000];[/color]i[COLOR=#990000]<=[/color][COLOR=#000080]NF[/color][COLOR=#990000];[/color]i[COLOR=#990000]++)[/color][b][COLOR=#000080]printf[/color][/b][COLOR=#FF0000]"%s%s"[/color][COLOR=#990000],[/color][COLOR=#009900]$i[/color][COLOR=#990000]?[/color][COLOR=#009900]$i[/color][COLOR=#990000]:[/color][COLOR=#FF0000]"_"[/color][COLOR=#990000],[/color]i[COLOR=#990000]%[/color][COLOR=#993399]4[/color][COLOR=#990000]?[/color][COLOR=#000080]OFS[/color][COLOR=#990000]:[/color][COLOR=#000080]ORS[/color][COLOR=#FF0000]}[/color]' /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