[GLLUG] Grep or ls or tail or something

Ben Pfaff blp@cs.stanford.edu
28 Jan 2002 00:17:52 -0800


Mike Szumlinski <szumlins@msu.edu> writes:

> Now, could you do myself and probably a couple other people on the list a
> favor and tell me what the hell all the slashes and brackets mean in this?
> 
> >       find . -type f | sed 's/\([^./a-zA-Z0-9_-]\)/\\\1/g;'

s/regexp/replacement/g is the sed command to replace a regexp by
another string.  Normally only the first match on each line is
replaced, but the `g' makes it "global", causing it to replace
all possible matches on a given line.

[abc] is a "character class" that matches any of the characters
between the brackets.  The initial ^ in the character class means
to match any character *not* listed in the brackets.

The \( and \) allow the stuff between them to be referred to as
\1 in the replacement text.  In order to put a literal backslash
in the replacement text, it has to be escaped by an extra
backslash.  So the replacement text consists of a double
backslash `\\', which gets translated to a single backslash,
followed by `\1', which gets replaced by the character matched by
the character class.

To summarize, the sed command takes any character that's not a
letter digit or one of . / _ - and puts a backslash in front of
it.
-- 
"...Slashdot is built on loyalty and trust, not on traditional
 business precepts."
--Robin Miller