From: a.abdulwahab@gmail.com on
Hello,

I am writing a shell script, (kshell), and I am using sed.

sed '_<head></head>_<head>Something</head>_' > $file

This works fine, and it replaces the text. The problem is that I want
to insert a java script , but its a big chunk, and the method above
doesnt work when it exceeds one line. It gives me "unterminated
command" , but I explicitly checked the separators, and the single
quotes. Any small example on how to use sed for big paragraphs. I'd
also appreciate an example on how to use sed to insert/append strings.

Thanks.

From: Ralf Fassel on
* "a.abdulwahab(a)gmail.com" <a.abdulwahab(a)gmail.com>
| use _ instead of / due to obvious reasons of html tags), and i checked,
| in my script there are no single quotes that can mess up the single
| quote matching.

Most probably there are underscores in your script which mess up the
sed separators. Try to use a separator which does not appear in your
script, or try 'awk' or 'perl' for stuff like this. sed is probably
not the right tool for this task.

R'
From: Chris F.A. Johnson on
On 2006-08-24, a.abdulwahab(a)gmail.com wrote:
> Hello ,
>
> I am writing a script to add stuff to html pages en-masse.Its a ksh
> script)
>
> I did this:
>
> sed '_</head>_</head><script>test</script>_' > $file
>
> and it works, it just replaces </head> with
></head><script>test</script> (which is just like inserting),

When you post code, please cut and paste it or insert the file
directly into your article. The script you posted would NOT work.

> but
> whenever I try to insert a big chunck it gives me "unmatched command"
> or unterminated, but I checked the single quotes, and the separators (i
> use _ instead of / due to obvious reasons of html tags), and i checked,
> in my script there are no single quotes that can mess up the single
> quote matching.
>
> Any example on how to do this but for multiple lines? I'd also
> appreciate a small example on using sed to insert/append.

I wouldn't use sed; sed is line oriented. It is possible to do
more, but the syntax is so cryptic as to make it very hard to
maintain. Like perl, sed is sometimes described as a write-only
language.

Often I would use awk, but for the example you give, I'd use a
pure shell script; </head> is not likely to be far into the file,
so the shell will be fast enough:

insert="Multiline
insert
here"

{
while IFS= read -r line
do
printf "%s\n" "$line"
case $line in
*"</head>"*) printf "%s\n""$insert"
break
;;
esac
done
cat
} < FILE > NEWFILE

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence