Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Command line scripts using SED - Substitution not working!

Status
Not open for further replies.

JaybOt

Programmer
Apr 18, 2001
101
GB
Hi all,

Not been in here fo a while, hope someone can help. Basiclly, i have some HTML files that i need to correct errors in. I want to be able to do run a script with some parameters that will produce a new HTML file, with subtitutions. e.g.

commad will be ...

# fix-code.scr $1 $2 $3

where $1 is string to find, $2 is what to replace it with, and $3 is the name of the input file.

The purpose of this script is realy to correct HTML docs produced by web page editors that have supplied an incorrect link to a file or image using the 'file:///' method, they should insert the file reletive to the HTML doc, but this is not always the case.

Here is my script so far ...

#!/bin/sh
/usr/bin/sed s/$1/$2/g $3 > ./$3.sed
#

Now, this works IF i am just replacing a word, so this ...

# fix-code.scr file mile test.html

.. works and produces a file called test.html.sed. Cool!

The problem is with using longer lines that contain '/', and ':' I need a way to handle the input correctrly.

If I do this ...

# fix-code.scr file:////box/dir/dir/public_html/index.html ../index.html test.html

.. which should replace lines that read 'file:///bo..' with '../index.html'

but I get an error ....

sed: 1: "s/file:////box/dir/dir/ ...": bad flag in substitute command: '/'

Any idea's where i may be going wrong, or how my code needs to be altered are welcomed.

Regards JayBot! "Always know what you say, but don't always say what you know!"
 
try using a different delimiter in your sed statement, you don't have to use /

s=$1=$2=g
 
WOW! that is something I never knew. From the man page:

... Any character that is displayed after the s subcommand can substitute for the / (slash) separator except for the space or new-line character.

I wonder if the same holds for vi. I'll have to test. The / (slash) character may be infrequently used in normal text documents, but web pages really over use that character. I hate having to go \/ (back-slash)(slash) when doing a global search and replace.

Thanks a ton - this forum is a god-send.
Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
Thanks chunky_monkey, that didi the trick! I was not aware of that feature as i have had little exposure to SED, i have only used it to do simple search and replace stuff.

Thanks again!

Jaybot ;-) "Always know what you say, but don't always say what you know!"
 
I would like to add a line to my file that do not have the following pattern eg:

0 1 999 therefore if I 'll find in my a pattern on where the third field doesn't start with a 1 ie: i would add a second entry
eg:

0 1 1999 etc...

Basically I would read a file F then check for the pattern 0 1 and the check for a leading one in the third field if its missing I would add anew line ... So my new file would have:

0 1 999
0 1 1999

I have tried doing it with sed but I did't succeed:

sed -e '1,$s/0 1 [2-9]*/0 1 1/' F

sos ... Can anybody help ??

Tks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top