Unix Shell - An Awk Error

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2006 > An Awk Error





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 An Awk Error
Steven Woody

2006-01-04, 6:27 pm

i got an input line,

0000000: 68 87 00 01 00 11 00 68 a1 00 00 0a 16 h......h.....

and want to use awk to convert it as,

<68><87><00><01><00><11><00><68><a1><00><00><0a><16>

to do that, i wrote a awk script:

{
for ( i = 2; i <=17; ++i )
if ( length( $i ) == 2 )
printf( "<%s>", $i );
printf("\n");
}

but it will report error when 'i' goes to 15. it seems the length()
can not handle "h......h....." string. why?

Chris F.A. Johnson

2006-01-04, 6:27 pm

On 2006-01-04, Steven Woody wrote:
> i got an input line,
>
> 0000000: 68 87 00 01 00 11 00 68 a1 00 00 0a 16 h......h.....
>
> and want to use awk to convert it as,
>
><68><87><00><01><00><11><00><68><a1><00><00><0a><16>
>
> to do that, i wrote a awk script:
>
> {
> for ( i = 2; i <=17; ++i )
> if ( length( $i ) == 2 )
> printf( "<%s>", $i );
> printf("\n");
> }
>
> but it will report error when 'i' goes to 15. it seems the length()
> can not handle "h......h....." string. why?


Your script runs without error for me.

How, exactly, are you calling the script?

What, exactly, is the error message?


--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Ed Morton

2006-01-04, 6:27 pm



Steven Woody wrote:
> i got an input line,
>
> 0000000: 68 87 00 01 00 11 00 68 a1 00 00 0a 16 h......h.....
>
> and want to use awk to convert it as,
>
> <68><87><00><01><00><11><00><68><a1><00><00><0a><16>
>
> to do that, i wrote a awk script:
>
> {
> for ( i = 2; i <=17; ++i )
> if ( length( $i ) == 2 )
> printf( "<%s>", $i );
> printf("\n");
> }
>
> but it will report error when 'i' goes to 15.

it seems the length()
> can not handle "h......h....." string. why?


What you posted won't actually produce an error message, but assuming
you really do have some kind of a problem (e.g. maybe you're trying to
do a division by "$i" in your real script) the problem with the above
code isn't with "length()", you just don't have that many fields.

Instead of using a hard-coded "17", use "NF" to control the loop.

Having said that, you don't need to loop through it anyway, this will do it:

awk -v OFS="><" '{$1="";$NF="";gsub(/^>|<$/,"")}1'

Regards,

Ed.


Steven Woody

2006-01-04, 8:49 pm


Ed Morton wrote:
> Steven Woody wrote:
> it seems the length()
>
> What you posted won't actually produce an error message, but assuming
> you really do have some kind of a problem (e.g. maybe you're trying to
> do a division by "$i" in your real script) the problem with the above
> code isn't with "length()", you just don't have that many fields.
>
> Instead of using a hard-coded "17", use "NF" to control the loop.
>
> Having said that, you don't need to loop through it anyway, this will do it:
>
> awk -v OFS="><" '{$1="";$NF="";gsub(/^>|<$/,"")}1'


thanks! it is handy of $NF. but, could you explain for me what is
the last '1' for ?



>
> Regards,
>
> Ed.


Steven Woody

2006-01-04, 8:49 pm


Chris F.A. Johnson wrote:
> On 2006-01-04, Steven Woody wrote:
>
> Your script runs without error for me.
>
> How, exactly, are you calling the script?
>
> What, exactly, is the error message?


sorry, when i today ceme to my office and run the same code on my
office computer, it does not report error. but i can not recall what
exactly error message was yesterday night when i scratching before my
home computer :-)


>
>
> --
> Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
> Shell Scripting Recipes: | My code in this post, if any,
> A Problem-Solution Approach | is released under the
> 2005, Apress | GNU General Public Licence


Ed Morton

2006-01-04, 8:49 pm

Steven Woody wrote:

> Ed Morton wrote:
>
>
>
> thanks! it is handy of $NF. but, could you explain for me what is
> the last '1' for ?


It's a true condition that invokes the default action of printing the
current record ($0). By the way, I could've saved a couple of characters:

awk -v OFS="><" '{$1=$NF="";gsub(/^>|<$/,"")}1'

Regards,

Ed.

Steven Woody

2006-01-05, 2:50 am


Ed Morton wrote:
> Steven Woody wrote:
>
>
> It's a true condition that invokes the default action of printing the
> current record ($0). By the way, I could've saved a couple of characters:
>
> awk -v OFS="><" '{$1=$NF="";gsub(/^>|<$/,"")}1'


understood. thank you! by the way, where you learn the AWK, in man
page? i found it's hard to learn. would you please intro some good
learning resources ( web articles are prefered, books are also ok) to
me?



>
> Regards,
>
> Ed.


Janis Papanagnou

2006-01-05, 2:50 am

NNTP-Posting-Host: p5496833e.dip0.t-ipconnect.de
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: online.de 1136439344 17487 84.150.131.62 (5 Jan 2006 05:35:44 GMT)
X-Complaints-To: abuse@einsundeins.com
NNTP-Posting-Date: Thu, 5 Jan 2006 05:35:44 +0000 (UTC)
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6b) Gecko/20031205 Thunderbird/0.4
X-Accept-Language: en-us, en
In-Reply-To: <1136438073.159241.29550@g49g2000cwa.googlegroups.com>
Xref: number1.nntp.dca.giganews.com comp.unix.shell:177038

Steven Woody wrote:
>
> by the way, where you learn the AWK, in man page?


I read the book of the authors (Aho, Weinberger, Kernighan), which is
concise and has small and larger examples (but it's quite expensive).[*]
Regularily reading this newsgroup also gives interesting insights.

> i found it's hard to learn.


I think awk is one of the most simple (albeit very powerful) language;
once you read about the basic principles it is quite easy, IMO. For the
"programming language" aspects (control structures, commands) it is very
helpful, even trivial, if you already know the C programming language.

> would you please intro some good
> learning resources ( web articles are prefered, books are also ok) to
> me?


I like the above book. But I also have some tutorial link in my bookmarks
(though never read that myself): http://www.grymoire.com/Unix/Awk.html
Then the GNU awk manual contains some tutorial besided its information
about gawk (http://www.gnu.org/software/gawk/manual/gawk.html).

Janis

[*] And I think you won't find the '1' pattern explained there. ;-)
Ed Morton

2006-01-05, 2:50 am

In-Reply-To: <1136438073.159241.29550@g49g2000cwa.googlegroups.com>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <0cydndApP-BLKCHeRVn-hQ@comcast.com>
Lines: 72
NNTP-Posting-Host: 67.184.16.28
X-Trace: sv3-G8U4CMEA0TaPmLRxoaVb/ 1z8sjWgSGKKTOCReHfi5ebePDeIx3bjISD0qNz9t
Awtm/ DIf2l3bAfjCmt!guQW01eQlVxzcrhBAlmM2yFvwH
6n3O/ em0fFuRBANf9Z3rDsfJ553PH5pKxDBIuwv7VPMPH
32lpI!NQ==
X-Complaints-To: abuse@comcast.net
X-DMCA-Complaints-To: dmca@comcast.net
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.32
Xref: number1.nntp.dca.giganews.com comp.unix.shell:177039

Steven Woody wrote:
> Ed Morton wrote:
>
>
>
> understood. thank you! by the way, where you learn the AWK, in man
> page? i found it's hard to learn. would you please intro some good
> learning resources ( web articles are prefered, books are also ok) to
> me?


I learned it mostly by lurking around comp.lang.awk and forcing myself
to use awk instead of the various other shell/sed solutions I was used to.

I used to use the Aho, etc. book,
http://cm.bell-labs.com/cm/cs/awkbook/ for reference, but I long ago
gave up on it in favor of Arnold Robins' book
http://www.oreilly.com/catalog/awkprog3/ (about the same time I really
started favoring GNU awk, gawk).

Ed.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com