[net.unix] sed question

jwp@uwmacc.UUCP (Jeffrey W Percival) (09/26/85)

sed(1) allows you to give the script on the command line like this:

	sed -e 'script' file1 > file2

Some of the sed commands, like a\ and i\ seem to need additional
lines of input.  I know that if I use the "-f sedfile" option I
can stash the commands in a file, but is there a way I can use
"append" and "insert" with the -e option?
-- 
	Jeff Percival ...!uwvax!uwmacc!jwp

solomon@uicsl.UUCP (09/30/85)

All sed commands are supposed to be on one line; therefore multiple line
insertions need to have the newline character escaped by use of the \.
Similarly, the shell assumes that a newline terminates the command, and
needs to have the newline character escaped via \.  Therefore, to use
append and insert with the -e option to sed, one must do something like

% sed -e '10a\\
haha\\
hoho\\
heehee\
' junk > morejunk

so that the shell reads a \, strips it off and ignores the newline character
following it, and thus passes a newline escaped with a \ to sed.  On the line
which inserts heehee, the shell passes an unescaped newline to sed, which
properly terminates the append command.

					Dilip V. Sarwate

....ihnp4!uiucdcs!uicsl!sarwate
....ihnp4!uiucdcs!uicsl!solomon
sarwate%uicsl.uucp@uiuc.arpa
solomon%uicsl.uucp@uiuc.arpa

jerryp@tektools.UUCP (Jerry Peek) (09/30/85)

In article <1492@uwmacc.UUCP> jwp@uwmacc.UUCP (Jeffrey W Percival) writes:
> sed(1) allows you to give the script on the command line like this:
> 
> 	sed -e 'script' file1 > file2
> 
> Some of the sed commands, like a\ and i\ seem to need additional
> lines of input.  I know that if I use the "-f sedfile" option I
> can stash the commands in a file, but is there a way I can use
> "append" and "insert" with the -e option?

Yes, but the way you do it depends on which shell you're using.
For example, in Bourne or Korn shells, you can do:
	$ sed -e '1a\
	> hi\
	> there' file1 > file2
	$
(the >'s are Bourne-shell secondary prompts).

To avoid "Unmatched '." errors in the C-shell, you have to use extra
backslashes:
	% sed -e '1a\\
	hi\\
	there' file1 > file2
	%
(in this case, csh won't give a secondary prompt).

--Jerry Peek, UNIX Training Instructor, Tektronix, Inc.
US Mail:    MS 74-222, P.O. Box 500, Beaverton, OR 97077
uucp:       {allegra,decvax,hplabs,ihnp4,ucbvax}!tektronix!tektools!jerryp
CS,ARPAnet: jerryp%tektools@tektronix.csnet
Phone:      503/627-1603

carl@bdaemon.UUCP (carl) (09/30/85)

> sed(1) allows you to give the script on the command line like this:
> 
> 	sed -e 'script' file1 > file2
> 
> Some of the sed commands, like a\ and i\ seem to need additional
> lines of input.  I know that if I use the "-f sedfile" option I
> can stash the commands in a file, but is there a way I can use
> "append" and "insert" with the -e option?
> -- 
> 	Jeff Percival ...!uwvax!uwmacc!jwp

Enter your commands like

	$ sed -e '
	> one or more lines
	> of script including
	> appends and inserts' file1 > file2

Note the UNIX secondary prompt and that you don't really need
the -e option.

Carl Brandauer
daemon associates, Inc.
1760 Sunset Boulevard
Boulder, CO 80302
303-442-1731
{allegra|amd|attunix|cbosgd|ucbvax|ut-sally}!nbires!bdaemon!carl

ken@turtlevax.UUCP (Ken Turkowski) (09/30/85)

In article <1492@uwmacc.UUCP> jwp@uwmacc.UUCP (Jeffrey W Percival) writes:
>sed(1) allows you to give the script on the command line like this:
>
>	sed -e 'script' file1 > file2
>
>Some of the sed commands, like a\ and i\ seem to need additional
>lines of input.  I know that if I use the "-f sedfile" option I
>can stash the commands in a file, but is there a way I can use
>"append" and "insert" with the -e option?

No problem.  Just don't try to do it with the C-shell.  Switch to the
Bourne shell first, open the script with a ' type in multiple lines,
then end it with a '.
-- 
Ken Turkowski @ CADLINC, Menlo Park, CA
UUCP: {amd,decwrl,hplabs,seismo,spar}!turtlevax!ken
ARPA: turtlevax!ken@DECWRL.ARPA

itkin@luke.UUCP (Steven List) (10/01/85)

In article <1492@uwmacc.UUCP> jwp@uwmacc.UUCP (Jeffrey W Percival) writes:
>sed(1) allows you to give the script on the command line like this:
>
>	sed -e 'script' file1 > file2
>
>Some of the sed commands, like a\ and i\ seem to need additional
>lines of input.  I know that if I use the "-f sedfile" option I
>can stash the commands in a file, but is there a way I can use
>"append" and "insert" with the -e option?

Yep.  First, use the Bourne shell. Then, type the command exactly as
it would appear in a sed command file.  The example I tried (so I
wouldn't sound like an idiot when I posted this) is:

% sh
$ echo hello | sed -e 'a\
there\
chum'
hello
there
chum

Pretty simple, huh?  Apparently the single quote escapes the backslash
escape so that sed sees it just as typed.  This same kind of thing holds
true for commands like echo:

$ echo "This is a multi-line
output example from echo"
This is a multi-line
output example from echo

This lets you create whole screenfulls of output with one invocation of
the echo command.
-- 
***
*  Steven List @ Benetics Corporation, Mt. View, CA
*  Just part of the stock at "Uncle Bene's Farm"
*  {cdp,greipa,idi,oliveb,sun,tolerant}!bene!luke!itkin
***

jpn@teddy.UUCP (10/01/85)

>> sed(1) allows you to give the script on the command line like this:
>> 
>> 	sed -e 'script' file1 > file2
>> 
>> Some of the sed commands, like a\ and i\ seem to need additional
>> lines of input.
>
>Enter your commands like
>
>	$ sed -e '
>	> one or more lines
>	> of script including
>	> appends and inserts' file1 > file2
>
Or, if you are a csh user:

       % sed -e '\
       one or more lines of script each terminated by a backslash \
       note: no further prompts will be issued \
       until a line without a backslash' file1 > file2

peter@graffiti.UUCP (Peter da Silva) (10/02/85)

> 	$ sed -e '
> 	> one or more lines
> 	> of script including
> 	> appends and inserts' file1 > file2

Or else:

% sed '\
? one or more lines\
? of script including\
? appends an inserts' file1 > file2

But be careful to double-backslash the appends and inserts.

Oh, for a good public-domain shell with shell functions!