For Programmers: Free Programming Magazines  


Home > Archive > AWK > January 2006 > set word length









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author set word length
venki.slm@gmail.com

2006-01-25, 7:55 am

Hi,

I want the result format of testcases as follows,

1) testcase1 : Testing long attribute :: Passed
2) testcase2 : Testing longlong attribute :: Passed
3) testcase3 : Testing double :: Passed
4) testcase4 : Testing long double attribute :: Failed


But it prints as,

1) testcase1 : Testing long attribute :: Passed
2) testcase2 : Testing longlong attribute :: Passed
3) testcase3 : Testing double :: Passed
4) testcase4 : Testing long double attribute :: Failed

I used the following wrapper awk, But I could not get the correct
format.
=================wrap.awk=========
BEGIN {if (MAXWIDTH == 0) MAXWIDTH = 80}
{
while (length($0) > MAXWIDTH)
{
for (k = MAXWIDTH; k > 2; --k)
if (substr($0,k,1) == " ") break
if (substr($0,k,1) == " ")
{
print substr($0,1,k-1)
$0 = PREFIX substr($0,k+1)
}
else # no breakpoint found
{
print $0
$0 = ""
}
}
print $0
}
=================================

Please help me.

Janis Papanagnou

2006-01-25, 6:56 pm

venki.slm@gmail.com wrote:
> Hi,
>
> I want the result format of testcases as follows,
>
> 1) testcase1 : Testing long attribute :: Passed
> 2) testcase2 : Testing longlong attribute :: Passed
> 3) testcase3 : Testing double :: Passed
> 4) testcase4 : Testing long double attribute :: Failed


Your output is ill-formatted; could it be that you use a broken
news-client that uses proportional instead of monospaced font?

Are you generating the output yourself or do you just want to
reformat existing output?

In the first case, instead of print, use the printf function and
define appropriate padding ("%40s" or whatever) for your fields.
In the second case you may, e.g., either use $NF and $(NF-1) to
handle the format of the last two fields as you want, or do some
pattern matching against the complete line using an end of line
anchor (e.g. /:: [^ ]+$/) with awk's match function.

Janis


> But it prints as,
>
> 1) testcase1 : Testing long attribute :: Passed
> 2) testcase2 : Testing longlong attribute :: Passed
> 3) testcase3 : Testing double :: Passed
> 4) testcase4 : Testing long double attribute :: Failed
>
> I used the following wrapper awk, But I could not get the correct
> format.
> =================wrap.awk=========
> BEGIN {if (MAXWIDTH == 0) MAXWIDTH = 80}
> {
> while (length($0) > MAXWIDTH)
> {
> for (k = MAXWIDTH; k > 2; --k)
> if (substr($0,k,1) == " ") break
> if (substr($0,k,1) == " ")
> {
> print substr($0,1,k-1)
> $0 = PREFIX substr($0,k+1)
> }
> else # no breakpoint found
> {
> print $0
> $0 = ""
> }
> }
> print $0
> }
> =================================
>
> Please help me.
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com