#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 ***
*******************************************************************************
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
>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. |
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