[net.unix] Neophyte awk question

simard@loral.UUCP (Ray Simard) (09/18/84)

[Eh?]

I needed an awk process to output an SOH character (binary 1).  When
I put the following in:

	{printf"%c",'\001'}

awk complained and stubbornly refused.  However, the exact same
thing in C (given the syntactical difference with parentheses)
works fine.  I understood that the 'printf' statement in awk
was the same as in C.  At any rate, I can't seem to get an SOH from
awk.

Also, I needed to do a lower-to-upper case conversion. All the mathematical
tricks failed and I wound up creating a string of the alphabet in 
upper, then lower case and using 'index' and  'substr' functions
to do it.  Is there a better way?

Many thanks for any ideas.
-- 
[     I am not a stranger, but a friend you haven't met yet     ]

Ray Simard
Loral Instrumentation, San Diego
{ucbvax, ittvax!dcdwest}!sdcsvax!sdcc6!loral!simard

liberte@uiucdcs.UUCP (09/25/84)

I discovered this awk-wardness a while ago and reported a bug fix
in net.bugs.4bsd to allow awk programs to use \nnn as well as
\f \b \r \n and \\ in all strings or character literals.  The problem
isnt in printf as it would seem since translation to the corresponding
ascii code is done during compilation of the strings, not during output.
I will resend my fix to whoever is interested.

One way to get around the problem without modifying `awk` is to
use something like:  printf "%c", 1.  However, this is often awkward.


Daniel LaLiberte          (ihnp4!uiucdcs!liberte)
U of Illinois, Urbana-Champaign, Computer Science
{limit your limits;  extend your extensions?}

henry@utzoo.UUCP (Henry Spencer) (09/25/84)

> I needed an awk process to output an SOH character (binary 1).  When
> I put the following in:
> 
> 	{printf"%c",'\001'}
> 
> awk complained and stubbornly refused.  However, the exact same
> thing in C (given the syntactical difference with parentheses)
> works fine.  I understood that the 'printf' statement in awk
> was the same as in C.  At any rate, I can't seem to get an SOH from
> awk.

Awk is not C, despite surface similarities and occasional rash
statements in the manual.  There are no single characters in awk,
just strings, so the single quotes are a no-no and the %c format
is meaningless.  More serious, alas, is that awk takes "\001" as
a string four characters long, so there isn't any good answer to your
problem.

> Also, I needed to do a lower-to-upper case conversion. All the mathematical
> tricks failed and I wound up creating a string of the alphabet in 
> upper, then lower case and using 'index' and  'substr' functions
> to do it.  Is there a better way?

The right way to do this in C is to avoid the slightly-unportable
mathematical tricks and use toupper() and tolower(); alas, awk doesn't
seem to have them.  The mathematical tricks don't work because awk does
not have C's tolerance for type punning, e.g. treating a character as
an integer.  I think the way you did it is the best available.
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,linus,decvax}!utzoo!henry

henry@utzoo.UUCP (Henry Spencer) (09/27/84)

> use something like:  printf "%c", 1.  However, this is often awkward.

Hm, one learns something every day.  How embarrassing, since my posting
to the net on the subject said it couldn't be done...

On inspection of the code, awk's %c takes the argument as a number (if
possible, otherwise you get nothing printed), converts it to an int, and
feeds that to the C %c.  By accident, the experiments I did with %c all
ran afoul of the value not being a number.

My general comment "awk is not C" stands, however.  Try using %u and
you'll see what I mean...
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,linus,decvax}!utzoo!henry

geoff@desint.UUCP (Geoff Kuenning) (10/09/84)

>> I needed an awk process to output an SOH character (binary 1).  When
>> I put the following in:
>> 
>> 	{printf"%c",'\001'}
>> 
>> awk complained and stubbornly refused.  However, the exact same
>> thing in C (given the syntactical difference with parentheses)
>> works fine.  I understood that the 'printf' statement in awk
>> was the same as in C.  At any rate, I can't seem to get an SOH from
>> awk.

>Awk is not C, despite surface similarities and occasional rash
>statements in the manual.  There are no single characters in awk,
>just strings, so the single quotes are a no-no and the %c format
>is meaningless.  More serious, alas, is that awk takes "\001" as
>a string four characters long, so there isn't any good answer to your
>problem.

Gee, on system V I just tried:

    awk '{printf ("%c", 7);}'


and got a bell for every line I typed, just like you would expect.  "%c"
prints an integer as a character;  awk just passes the format and the integer
to C's printf.  Note the correct syntax in the example above;  "printf" in
awk *does* require parentheses--it's "print" that doesn't, just to keep life
confusing.
-- 
	Geoff Kuenning
	First Systems Corporation
	...!ihnp4!trwrb!desint!geoff

achut@callan.UUCP (Achut Reddy) (10/09/84)

Hold on there.  awk doesn't *require* parentheses for printf;
spaces will do just fine.

For more fun with awk, try the following:

awk 'BEGIN { printf "The correct spelling is %ardvark\n" }' < /dev/null
awk 'BEGIN { printf "This is your lucky number: %d\n", 123456789 }' < /dev/null

(May not work on all versions of awk)


"With awk, you can slice, serve, smash... you can do everything with awk!"

"awk is your friend!"
-- 
	Achut Reddy @ Callan Data Systems
	..!ihnp4!wlbr!callan!achut

henry@utzoo.UUCP (Henry Spencer) (10/10/84)

> ...  Note the correct syntax in the example above;  "printf" in
> awk *does* require parentheses--it's "print" that doesn't, just to keep life
> confusing.

If your awk needs parentheses around the parameters to printf, your awk
is broken.  The V7 awk doesn't need the parentheses, the examples in the
V7 awk manual don't have them, and we've never used them anywhere in our
(fairly extensive) body of awk code.

As I admitted in my earlier followup message, %c in awk does work; I goofed.
-- 
				Henry Spencer @ U of Toronto Zoology
				{allegra,ihnp4,linus,decvax}!utzoo!henry