Unix Programming - sed

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2006 > sed





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author sed
a.abdulwahab@gmail.com

2006-08-24, 1:38 pm

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), 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.

Thanks!

Ralf Fassel

2006-08-24, 1:38 pm

* "a.abdulwahab@gmail.com" <a.abdulwahab@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'
Chris F.A. Johnson

2006-08-25, 1:40 am

On 2006-08-24, a.abdulwahab@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
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com