Subject: Re: shell script question (grep)
To: Lubos Vrbka <shnek@chemi.muni.cz>
From: Giorgos Keramidas <keramida@ceid.upatras.gr>
List: netbsd-help
Date: 02/12/2003 03:08:28
On 2003-02-11 19:34, Lubos Vrbka <shnek@chemi.muni.cz> wrote:
> > You could force sed to treat $i and $j as full "words" by adding
> > \<...\> around them in the substitution pattern.  Seeing how you pass
> > $i as the last part of a line, you don't need the \> part either, and
> > that would make the script:
> >
> > sed -e "s/\\<$i\$/$j/" < file1 > file1.out
> why is there a double backslash in the beginning?

To make sure that the backslash character is passed literally as part
of the sed argument.

> does the "full word" mean that it has to be separated by some white
> character from both sides (or, in this case, by space from left and
> EOLN (\$) from right)?

I remember the \< and \> sequences from ed(1) usage.  Regexps are
described in detail in re_format(7).  Looking now, re_format(7) seems
to say that this is written slightly different manner, but anyway.
The definition of a word is `[[:alnum:]_]\+' that is, alphanumeric
characters or underscores.

> how could i include a dollar sign to the string specification so it
> is no more control character? \\$ ?

That depends on the quoting rules of your shell.  I assumed /bin/sh
and sorry for not mentioning it before.  In /bin/sh, the double quote
character allows expansion of escape sequences like \$, so to include
a literal '$' character you have to write "\$" and to write a literal
backslash character "\\", i.e.:

	$ PS1='sh> '
	sh> echo "\$"
	$
	sh> echo "\"
	> ^C
	sh> echo "\\"
	\
	sh>

- Giorgos