Re: a SED need



On Tue, Dec 27, 2005 at 09:18:56AM -0600, Jack Stone wrote:
> I have some HTML files with hundreds of URLs that I need to modify using a
> search/replace string. I assume that SED(1) is the right tool to use, but
> every syntax I've tried has not worked.
>
> Here is what I'm trying to do:
> Change full URLs to relative paths, in other words, chop off the
> "http://www.example.com/"; portion:
>
> >From this:
> <li><a href="http://www.example.com/model/many.html";>
> To this:
> <li><a href="model/many.html">
>
> I think it is the slashes and quotes that are giving me fits as I'm very
> much a novice on SED(1) syntax.

Am sure sed is the right high power production tool for getting the job
done but I get such things done easier in awk. Am sure many say the same
about perl. Sed, awk, perl, is the evolutionary order.

Save this as something like "example.awk" and chmod +x to make it
executable for easy reuse. Or you could "awk -f example.exe input >
output"

By saving to a file you bypass the need to escape characters from the
shell (which will be different depending on csh vs. sh) and yet again
from the RE parser. The escapes below are to make sure the literal
character is used for regular expression rather than a possible RE
interpretation.

Contains two patterns to match. The first matches the thing you are
looking to change. The match regular expression is repeated in gsub()
where its replaced with the plain text you desire. "Print" causes the
line to be outputed, and "next" ends the processing of that input line
so the next pattern isn't tried. Therefore the next match-all pattern
prints everything the first skipped.

#!/usr/bin/awk -f

/<a href=\"http:\/\/www.example.com\// {
gsub(/<a href=\"http:\/\/www.example.com\//, "<a href=\"")
print
next
}

{ print }


--
David Kelly N4HHE, dkelly@xxxxxxxxxx
========================================================================
Whom computers would destroy, they must first drive mad.
_______________________________________________
freebsd-questions@xxxxxxxxxxx mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questions-unsubscribe@xxxxxxxxxxx"



Relevant Pages

  • Re: Regular Expression
    ... Contains a regular expression pattern. ... If you use Syntax 1, delimit the pattern by "/" characters. ... Enclose switch in quotation marks if you use Syntax 2. ...
    (microsoft.public.scripting.jscript)
  • Re: Regular Expression
    ... Contains a regular expression pattern. ... If you use Syntax 1, delimit the pattern by "/" characters. ... Enclose switch in quotation marks if you use Syntax 2. ...
    (microsoft.public.scripting.jscript)
  • regular expression ??
    ... I have been recently working with regular expression and following the ... syntax per several sources including Microsoft.com ... a for next loop the Bmatch.value will be the pattern from re.pattern.I want ...
    (microsoft.public.scripting.vbscript)
  • Regex help
    ... Can someone help me out with the syntax of a regular expression ... which searches a string for the pattern ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: regular expression question
    ... "Syntax" means nothing to Regular ... public string Opened() ... and be able to express them in Regular Expression syntax. ... of word characters, and as I said before, '<' is not a word character. ...
    (microsoft.public.dotnet.general)