Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Attn UNIX users (OT)

Author: Russell Reagan

Date: 19:09:15 01/24/04

Go up one level in this thread


On January 24, 2004 at 21:25:44, Matthew Hull wrote:

>Find some HTML file and process it with the following command:
>
>sed -e s/\<[^\>]*\>//g somefile.htm
>
>Now put the above sed command in a file called "test" and do the following:
>
>sed -f test somefile.htm
>
>Question:  Is the output for both methods identical on your machine?
>
>If not, why not?


You need to protect the sed expression from the command line interpreter, like
this:

sed 's/\<[^\>]*\>//g' somefile.htm

The command line interpreter (bash, or whatever you're using) treats certain
characters as special. For instance, the character * expands to a file listing
of the current directory. When you did \<, the shell converted that to just <.
To see how this works, type these:

echo *
echo \<\>

There are three ways to protect something from being interpreted by the shell.
The first way, and weakest way, is to use double quotes, like this:

sed "s/\<[^\>]*\>//g" somefile.htm

The reason that is the weakest is because it still allows for shell variable
expansion. For instance:

$ foo=bar
$ echo $foo
bar
$ echo "$foo"
bar
$ echo '$foo'
$foo

Single quotes will protect shell variable expansion, as in the example above.
The last way is to use a backslash, which protects the character immediately
following it:

echo \$foo



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.