Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem with 'sed' (command garbled)!!

326 views
Skip to first unread message

Victor Abel Garcia Palacios

unread,
Apr 14, 1994, 10:46:08 AM4/14/94
to

#In a shell script I want to insert a text with several lines in the second
#line of a file. But IT DOESN'T WORK!!
#The code is somenthing like this:

ins="What's
wrong
in this
script?"

sed "2 i//
$ins" /tmp/file

ERROR:
sed: command garbled: 2 i//
# Note that the text is inside a variable. I have read the manual of the command
#'sed' and all is correct!!!.
# Could someone tell me what is wrong or other way to do this.
# Thanks for read this question.

--

*******************************************************************************
*** Victor Abel Garcia Palacios | A A ***
*** vga...@vents.uji.es | M G ***
*** Universitat Jaume I | I RULEZ!!! ***
*** Castellon SPAIN | M G ***
*** | A A ***
*******************************************************************************

Rudolf Holzner

unread,
Apr 18, 1994, 11:02:10 AM4/18/94
to

In article <2ojkvg$l...@pereiii.uji.es>, vga...@vents.uji.es (Victor Abel Garcia Palacios) writes:
|>
|>
|> #In a shell script I want to insert a text with several lines in the second
|> #line of a file. But IT DOESN'T WORK!!
|> #The code is somenthing like this:
|>
|> ins="What's
|> wrong
|> in this
|> script?"
|>
|> sed "2 i//
|> $ins" /tmp/file
|>
|> ERROR:
|> sed: command garbled: 2 i//
|> # Note that the text is inside a variable. I have read the manual of the command
|> #'sed' and all is correct!!!.
|> # Could someone tell me what is wrong or other way to do this.
|> # Thanks for read this question.
|>

ins="That's\\
wrong\\
in this\\
script!"

sed "2 i\\
$ins" /tmp/file


Every inserted line must end with a backquote '\'. Only the final
line has no backquote. Otherwise sed cannot distinguish, what is a
sed command (here i) and what is the argument for this command.

Rudi

Jonathan H. N. Chin

unread,
Apr 19, 1994, 12:29:06 PM4/19/94
to
vga...@vents.uji.es (Victor Abel Garcia Palacios) writes:

>ins="What's
>wrong
>in this
>script?"
>
>sed "2 i//
>$ins" /tmp/file

># I have read the manual of the command


>#'sed' and all is correct!!!.

Not quite.
You missed out all the escapes (`\').
The `i' command is followed by a backslash, as must be all
but the last line of text inserted.

Thus:

#!/bin/sh

ins="Nothing's\\
wrong\\
in this\\
script"

sed "2 i\\
$ins" /tmp/file

where the `\\' escapes the backslash from the shell
(ie. sed only sees a single backslash).


-jonathan

--
Jonathan H N Chin, 4 kyu | Cybernetics / CompSci | "Respondeo, etsi mutabor"
| University of Reading |
shr...@reading.ac.uk | Box 225, Whiteknights | < Rosenstock-Huessy >
cyb...@cyber.rdg.ac.uk | Reading, RG6 2AY, UK. |

Conrad Kimball

unread,
Apr 20, 1994, 7:45:00 PM4/20/94
to
In article <2ojkvg$l...@pereiii.uji.es>, vga...@vents.uji.es (Victor Abel Garcia Palacios) writes:
|>
|>
|> #In a shell script I want to insert a text with several lines in the second
|> #line of a file. But IT DOESN'T WORK!!
|> #The code is somenthing like this:
|>
|> ins="What's
|> wrong
|> in this
|> script?"
|>
|> sed "2 i//
|> $ins" /tmp/file
|>
|> ERROR:
|> sed: command garbled: 2 i//
|> # Note that the text is inside a variable. I have read the manual of the command
|> #'sed' and all is correct!!!.
|> # Could someone tell me what is wrong or other way to do this.
|> # Thanks for read this question.

Problem #1: use backslashes (\) instead of slashes (/). E.g.

sed "2i\\
$ins" /tmp/file

Problem #2: the "i" command can only insert a single line of text,
unless all but the last line is escaped with a backslash
before the newline. E.g.

ins="What's\\
wrong\\
in this\\
script?"

Thus, the following works just fine:

ins="Nothing\\
is wrong\\
in this\\
script!"

sed "2i\\
$ans" /tmp/file

--
Conrad Kimball | Client Server Tech Services, Boeing Computer Services
c...@sdc.cs.boeing.com | P.O. Box 24346, MS 7M-HC
(206) 865-6410 | Seattle, WA 98124-0346

0 new messages