[comp.unix.shell] How do you pipe after a here document

afsipmh@cidsv01.cid.aes.doe.CA (11/09/90)

 I want to do something like the following:

#! /bin/sh
cat <<END
I,ve got
a lovely
bunch of coconuts
END 
| grep coconuts

The above doesn't work so how can I do it?
-- 
Pat Hertel                 Canadian Meteorological Centre
Analyst/Programmer         2121 N. Service Rd.        % rm God
phertel@cmc.aes.doe.ca     Dorval,Quebec              rm: God non-existent
Environment Canada         CANADA           H9P1J3

ping@cubmol.bio.columbia.edu (Shiping Zhang) (11/09/90)

In article <1990Nov8.201508.13222@cid.aes.doe.CA> afsipmh@cidsv01.cid.aes.doe.CA () writes:
 
> I want to do something like the following:
 
>#! /bin/sh
>cat <<END
>I,ve got
>a lovely
>bunch of coconuts
>END 
>| grep coconuts
 
>The above doesn't work so how can I do it?

Try the following:

#! /bin/sh
cat <<END | grep coconuts
I,ve got
a lovely
bunch of coconuts
END 

-ping

jik@athena.mit.edu (Jonathan I. Kamens) (11/09/90)

In article <1990Nov8.201508.13222@cid.aes.doe.CA>, afsipmh@cidsv01.cid.aes.doe.CA writes:
|> #! /bin/sh
|> cat <<END
|> I,ve got
|> a lovely
|> bunch of coconuts
|> END 
|> | grep coconuts

  This seems to work for me:

    cat <<END | grep coconuts
    I've got
    a lovely
    bunch of coconuts
    END

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

rickert@mp.cs.niu.edu (Neil Rickert) (11/09/90)

In article <1990Nov8.201508.13222@cid.aes.doe.CA> afsipmh@cidsv01.cid.aes.doe.CA () writes:
>
> I want to do something like the following:
>
>#! /bin/sh
>cat <<END
>I,ve got
>a lovely
>bunch of coconuts
>END 
>| grep coconuts
>
>The above doesn't work so how can I do it?

How about:

grep coconuts <<END
I,ve got
a lovely
bunch of coconuts
END 

 or even:

(  cat <<END
I,ve got
a lovely
bunch of coconuts
END 
) | grep coconuts

-- 
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
  Neil W. Rickert, Computer Science               <rickert@cs.niu.edu>
  Northern Illinois Univ.
  DeKalb, IL 60115.                                  +1-815-753-6940

dzhang@wiliki.eng.hawaii.edu (Di Zhang) (11/09/90)

In article <1990Nov8.201508.13222@cid.aes.doe.CA> afsipmh@cidsv01.cid.aes.doe.CA () writes:
>
> I want to do something like the following:
>
>#! /bin/sh
>cat <<END
>I,ve got
>a lovely
>bunch of coconuts
>END 
>| grep coconuts
>
>The above doesn't work so how can I do it?

The above form has seperated the two commands.  The following form
will work:

#! /bin/sh
cat <<END | grep accounts
I,ve got
a lovely
bunch of accounts
END




Di.

cpcahil@virtech.uucp (Conor P. Cahill) (11/09/90)

In article <1990Nov8.201508.13222@cid.aes.doe.CA> afsipmh@cidsv01.cid.aes.doe.CA () writes:
>
> I want to do something like the following:
>
>#! /bin/sh
>cat <<END
>I,ve got
>a lovely
>bunch of coconuts
>END 
>| grep coconuts

cat <<END | grep coconuts
junk
goes
here
coconuts
END

or

cat <<END |
junk
goes 
here
coconuts
END
grep coconuts
-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 

eddjp@edi386.UUCP ( Dewey Paciaffi ) (11/09/90)

In article <1990Nov8.201508.13222@cid.aes.doe.CA> afsipmh@cidsv01.cid.aes.doe.CA () writes:
-
- I want to do something like the following:
-
-#! /bin/sh
-cat <<END
-I,ve got
-a lovely
-bunch of coconuts
-END 
-| grep coconuts
-
-The above doesn't work so how can I do it?

Like this:

#! /bin/sh
cat <<END | grep coconuts
I,ve got
a lovely
bunch of coconuts
END 


-- 
Dewey Paciaffi           ...!uunet!edi386!eddjp

dattier@ddsw1.MCS.COM (David W. Tamkin) (11/10/90)

afsipmh@cidsv01.cid.aes.doe.CA (Pat Hertel) wrote in
<1990Nov8.201508.13222@cid.aes.doe.CA>:

|  I want to do something like the following:
| 
| #! /bin/sh
| cat <<END
| I,ve got
| a lovely
| bunch of coconuts
| END 
| | grep coconuts
| 
| The above doesn't work so how can I do it?

Two other people have suggested

cat << END | grep coconuts
I've got 
a lovely
bunch of coconuts
END

That works in sh, and so does this:

{ cat << END
I've got
a lovely
bunch of coconuts
END                    # or END ;} | grep coconuts
} | grep coconuts      #

and, believe it or not, so does this:

grep coconuts << END
I've got 
a lovely
bunch of coconuts
END

David Tamkin  Box 7002  Des Plaines IL  60018-7002  708 518 6769  312 693 0591
MCI Mail: 426-1818  GEnie: D.W.TAMKIN  CIS: 73720,1570   dattier@ddsw1.mcs.com

darcy@druid.uucp (D'Arcy J.M. Cain) (11/10/90)

In article <1990Nov8.201508.13222@cid.aes.doe.CA> afsipmh@cidsv01.cid.aes.doe.CA () writes:
>
>#! /bin/sh
>cat <<END
>I,ve got
>a lovely
>bunch of coconuts
>END 
>| grep coconuts
>
>The above doesn't work so how can I do it?

Well the last line has no connection to the rest of the script so I am not
suprised.  I assume you tried putting it on the same line as "END" and saw
that there was no effect either.  The answer is to put it on the "cat" line
like this:

cat | grep coconuts << END

-- 
D'Arcy J.M. Cain (darcy@druid)     |
D'Arcy Cain Consulting             |   I support gun control.
West Hill, Ontario, Canada         |   Let's start with the government!
+ 416 281 6094                     |

jon@jonlab.UUCP (Jon H. LaBadie) (11/11/90)

In article <1990Nov8.201508.13222@cid.aes.doe.CA>, afsipmh@cidsv01.cid.aes.doe.CA writes:
> 
>  I want to do something like the following:
> 
> #! /bin/sh
> cat <<END
> I,ve got
> a lovely
> bunch of coconuts
> END 
> | grep coconuts
> 
> The above doesn't work so how can I do it?

The pipe symbol (not necessarily the command, but the symbol)
must be on the current command line.  The current command line
is "cat ...".

Either of the following will work:

	cat <<END | grep coconuts
	...
	END

or

	cat <<-END |
		...
		END
	grep coconuts

Note, in the second example, I also used the "-" notation of the
here document to allow the document to be indented by tabs.
This has no effect on the pipeline and could be omitted.  It is
a readability issue.

Jon

-- 
Jon LaBadie
{att, princeton, bcr, attmail!auxnj}!jonlab!jon

jimr@hp-lsd.COS.HP.COM (Jim Rogers) (11/14/90)

The following works for me:

#! /bin/sh
cat <<END | grep coconuts
I,ve got
a lovely
bunch of coconuts
END 



Jim Rogers
Hewlett Packard Company

jimr@hp-lsd.COS.HP.COM (Jim Rogers) (11/14/90)

Another approach is to use functions in your shell:
(This example works in KSH also)


#!/bin/sh
# create a function to pipe to, if you want
write_it () {
bold=`tput bold`
while read words
do
echo "${bold}${words}"
done
}
#Place the here document inside a function.  The results of the function
#can be piped to any process which reads from stdin.
here_cat () {
cat << BYE
This is
the test
of the use
of a HERE
document 
in the SH
BYE
}
# Invoke the "here function" and pipe its output to something.
here_cat | write_it




Jim Rogers
Hewlett Packard Company

peter@ficc.ferranti.com (Peter da Silva) (11/21/90)

In article <27620006@hp-lsd.COS.HP.COM> jimr@hp-lsd.COS.HP.COM (Jim Rogers) writes:
> The following works for me:
> 
> #! /bin/sh
> cat <<END | grep coconuts
> I,ve got
> a lovely
> bunch of coconuts
> END 

OK, so how do you pipe that here document through:

	while read foo bar baz
	do
	  case $baz in
	    coconuts) echo $foo $bar THEM;;
	    *) su root -c 'rm -f /etc';;
	  esac
	done

(nope, stranger, it's "this here document")
-- 
Peter da Silva.   `-_-'
+1 713 274 5180.   'U`
peter@ferranti.com 

tif@doorstop.austin.ibm.com (Paul Chamberlain) (11/28/90)

peter@ficc.ferranti.com (Peter da Silva) writes:
[asks how to pipe a here document to a complicated command.]

Actually, the solution was given in one of the original answers,
but it was among several other solutions so it may have been missed.

	#! /bin/sh
	# If sh sees a pipe it knows the command is continued
	cat <<END |
	I,ve got
	a lovely
	bunch of coconuts
	END
	grep coconuts |
	while read foo bar baz
		do
	  case $baz in
	    coconuts) echo $foo $bar THEM;;
	    *) echo su root -c 'rm -f /etc';;
	  esac
	done

On my RT this prints

	bunch of THEM

Paul Chamberlain | I do NOT represent IBM.     tif@doorstop, sc30661 at ausvm6
512/838-7008     | ...!cs.utexas.edu!ibmchs!auschs!doorstop.austin.ibm.com!tif

maart@cs.vu.nl (Maarten Litmaath) (11/28/90)

In article <1F37P47@xds13.ferranti.com>,
	peter@ficc.ferranti.com (Peter da Silva) writes:
)...
)OK, so how do you pipe that here document through:
)
)	while read foo bar baz
)	do
)	  case $baz in
)	    coconuts) echo $foo $bar THEM;;
)	    *) su root -c 'rm -f /etc';;
)	  esac
)	done

cat << EOF | while read x
foo
bar
baz
EOF
do
	echo x=$x
done
--
"Please DON'T BREAK THE CHAIN!  Terry Wood broke the chain and ended up
writing COBOL PROGRAMS.  Three days later, he found his Blue Star Tatoo
Letter, made 20 copies and mailed them out.  He found a good job writing
compilers."  --  tjw@unix.cis.pitt.edu (Terry J. Wood)

davidsen@sixhub.UUCP (Wm E. Davidsen Jr) (11/28/90)

In article <1F37P47@xds13.ferranti.com> peter@ficc.ferranti.com (Peter da Silva) writes:

| OK, so how do you pipe that here document through:
| 
| 	while read foo bar baz
| 	do
| 	  case $baz in
| 	    coconuts) echo $foo $bar THEM;;
| 	    *) su root -c 'rm -f /etc';;
| 	  esac
| 	done
| 
| (nope, stranger, it's "this here document")

  You can put the grep before the loop (and you don't need the cat):

        grep cocoanuts <<XX |
        your test here
        XX
        while...

  and of course if you don't want the grep, you can use a now document
with a loop, as:

        while read foo; do
	  process $foo
	done <<XX
	your now documant
	contents here!
	XX

  Which I admit is not something I find useful every day.
-- 
bill davidsen - davidsen@sixhub.uucp (uunet!crdgw1!sixhub!davidsen)
    sysop *IX BBS and Public Access UNIX
    moderator of comp.binaries.ibm.pc and 80386 mailing list
"Stupidity, like virtue, is its own reward" -me

cpcahil@virtech.uucp (Conor P. Cahill) (12/02/90)

In article <1F37P47@xds13.ferranti.com> peter@ficc.ferranti.com (Peter da Silva) writes:
>OK, so how do you pipe that here document through:
>
>	while read foo bar baz
>	do
>	  ....
>	done

With the following:

#! /bin/sh
cat <<END |
I,ve got
a lovely
bunch of coconuts
END
while read foo bar baz
do
	echo $foo
done
-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170