[comp.unix.questions] cascading pipes in awk

psm@manta.NOSC.MIL (Scot Mcintosh) (05/24/89)

Is there a way to cascade pipes in awk? I'm trying to do something
like the following:
    { print | "tr [a-z] [A-Z]" | <<other stuff that will manipulate the
				   now-upper-case text >>
    }

Any other suggestions to accomplish the same thing would be
appreciated.

hager@ksuvax1.cis.ksu.edu (Donald E. Hager) (05/24/89)

In article <813@manta.NOSC.MIL> psm@manta.nosc.mil.UUCP (Scot Mcintosh) writes:
>Is there a way to cascade pipes in awk? I'm trying to do something
>like the following:
>    { print | "tr [a-z] [A-Z]" | <<other stuff that will manipulate the
>				   now-upper-case text >>
>    }
>
>Any other suggestions to accomplish the same thing would be
>appreciated.

I've been reading this newsgroup for quite some time now, but I've never
posted before.  Here we go...

In reference to your <<other stuff...>> inside the awk program, I am
assuming that you are talking about more awk statements.  The problem
is that only one output statement to a pipe is permitted in an awk
program.  The solution that I see is to pipe your translated text into
an awk program:
	cat filename | tr [a-z] [A-Z] | awk '<<other stuff...>>'

I hope this helps!
--
Donald Hager					|    // //  =====   //   //
KSU Dept. of Computing & Information Sciences	|   // //  //___   //   //
Internet: hager@ksuvax1.cis.ksu.edu		|  //=<<      //  //   //
UUCP:  ...!{rutgers,texbell}!ksuvax1!hager	| //  //  =====   ======

psm@manta.NOSC.MIL (Scot Mcintosh) (05/24/89)

In article <1962@deimos.cis.ksu.edu> hager@ksuvax1.cis.ksu.edu (Donald E. Hager) writes:
>The problem
>is that only one output statement to a pipe is permitted in an awk
>program.  The solution that I see is to pipe your translated text into
>an awk program:
>	cat filename | tr [a-z] [A-Z] | awk '<<other stuff...>>'

Unfortunately, I only want to uppercase a few selected portions of the
text my awk program is reading (my original posting contained a
very simplified example, so this wasn't obvious). There just doesn't
seem to be a way to have a filter program in the middle of two groups
of awk statements.

Newsgroups: comp.unix.questions
Subject: Re: cascading pipes in awk
Summary: 
Expires: 
References: <813@manta.NOSC.MIL> <1962@deimos.cis.ksu.edu>
Sender: 
Reply-To: psm@manta.nosc.mil.UUCP (Scot Mcintosh)
Followup-To: 
Distribution: usa
Organization: Naval Ocean Systems Center, San Diego
Keywords: awk

rfinch@caldwr.UUCP (Ralph Finch) (05/25/89)

In article <813@manta.NOSC.MIL>, psm@manta.NOSC.MIL (Scot Mcintosh) writes:
> Is there a way to cascade pipes in awk? I'm trying to do something
> like the following:
>     { print | "tr [a-z] [A-Z]" | <<other stuff that will manipulate the
> 				   now-upper-case text >>
>     }
> 
> Any other suggestions to accomplish the same thing would be
> appreciated.

I'm also frustrated that awk doesn't allow the use of Unix utilities
inside of awk.

I had a similar problem that Scot did (only I wanted to go from upper
to lower case).  Here was my solution:

name_up="UPPERCASENAME"

# create upper-to-lower table
for (i=65;i<=90;i++) {
	up=sprintf("%c",i)
	lw=sprintf("%c",i+32)
	alpha[up]=lw
}
# preserve punctuation characters
for (i=32;i<=64;i++) {
	c=sprintf("%c",i)
	alpha[c]=c
}
for (i=91;i<=126;i++) {
	c=sprintf("%c",i)
	alpha[c]=c
}

# translate the name from upper-to-lower case
name_low=""
for (i=1; i<=length(name_up); i++)
	name=name""alpha[substr(name_up,i,1)]
-- 
Ralph Finch
...ucbvax!ucdavis!caldwr!rfinch

les@chinet.chi.il.us (Leslie Mikesell) (05/26/89)

In article <496@caldwr.UUCP> rfinch@caldwr.UUCP (Ralph Finch) writes:
>> Is there a way to cascade pipes in awk? I'm trying to do something
>> like the following:
>>     { print | "tr [a-z] [A-Z]" | <<other stuff that will manipulate the
>> 				   now-upper-case text >>
>>     }
>> 
>> Any other suggestions to accomplish the same thing would be
>> appreciated.
>
>I'm also frustrated that awk doesn't allow the use of Unix utilities
>inside of awk.

And I'm amazed that no one has suggested using perl instead, since
  (a) it can run other programs via shell-like `prog ` expansion, but
      wouldn't need to in this case since 
  (b) it has the tr as a built-in command, also named y as in sed.

And, it comes with a utility to translate your awk program, although
it has seldom worked for me because my awk programs tend to be written
as shell "here documents" so some of the work can be done by other
programs.
BTW, does anyone know why such an inherently unix-ish concept as ` `
expansion was omitted from awk? 

Les Mikesell