[comp.unix.questions] parsing commands in a makefile

clyne@redcloud.ucar.edu (John Clyne) (10/21/89)

Sorry if this has been beat to death before, but here goes anyway. 
I have a makefile that I want to use a sed command as a rule. I need to 
do an sed append. The syntax for this with sed is something like:

	% sed -e "/patten/a\
	text to append" < in_file > out_file

The newline after the 'a' must be passed to sed. On the shell command line 
this is easy. You just escape the newline with a '\'. The problem is 
in a makefile all rules must be contained on a single line. If you try
something like the following in a makefile:

target:
	sed -e "/pattern/a\
	text to append" < in_file > out_file

What sed gets passed is "/pattern/atext to append" Is there any way around 
this? i.e how can I pass a new line to a command that is part of a make rule?

thanks much - jc


 
	John Clyne 	(clyne@ncar.ucar.edu)
	c/o
	National Center for Atmospheric Research
	P.O. Box 3000
	Boulder, Colorado 80307
	(303) 497-1236

	
		%%% Its a small world. But I wouldn't want to paint it %%%
						S. Wright
		%%%						       %%%

maart@cs.vu.nl (Maarten Litmaath) (10/21/89)

clyne@redcloud.ucar.edu (John Clyne) writes:
\...
\target:
\	sed -e "/pattern/a\
\	text to append" < in_file > out_file
\
\What sed gets passed is "/pattern/atext to append" Is there any way around 
\this? i.e how can I pass a new line to a command that is part of a make rule?

Why don't you put the sed script into a file of its own?

	target:
		sed -f script.sed < in_file > out_file

Else you could set an environment variable, to be used in the Makefile:

	$ NL='
	'
	$ export NL
	$ cat Makefile
	SHELL	= /bin/sh
	target:
		sed -e '/pattern/a\$(NL)text to append' ...
	$
-- 
A symbolic link is a POINTER to a file, | Maarten Litmaath @ VU Amsterdam:
 a hard link is the file system's GOTO. | maart@cs.vu.nl, mcsun!botter!maart

chris@mimsy.umd.edu (Chris Torek) (10/21/89)

In article <4782@ncar.ucar.edu> clyne@redcloud.ucar.edu (John Clyne) writes:
>... The syntax for this with sed is something like:
>
>	% sed -e "/patten/a\
>	text to append" < in_file > out_file

(Note: you need two backslashes in csh.  It would be better to show the
above as `$ sed -e ...'.)

>... The problem is in a makefile all rules must be contained on a single
>line. If you try something like the following in a makefile:
>
>target:
>	sed -e "/pattern/a\
>	text to append" < in_file > out_file
>[it fails; is there a workaround?]

The only way to do this is indirectly.  For instance:

	sed -e `echo '/pattern/a\Xtext to append' | tr X '\012'`

If you are using the !@* Sys5 `interpreting echo', you need to double
the backslash.  Also, neither `pattern' nor `text to append' can contain
an `X'.

In general, it is easier to put the `sed' command(s) into a separate
file (either with `sh cmdfile.sh' or `sed -f file.of.sed.commands').
-- 
`They were supposed to be green.'
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris