RE's and grep

Chris Devers cdevers at pobox.com
Tue Sep 2 15:51:28 EDT 2003


On Tue, 2 Sep 2003, Dan Barrett wrote:

> On Tuesday 02 September 2003 15:35, Derek Martin wrote:
>
> > If the file contains more than one word per line (such as a dictionary
> > with definitions), this won't work.  It can still be done, but you'd
> > need a lot more logic.  AFAIK, there's no way to do that with a single
> > regular expression, or even a simple pipeline.
>
>
> In this case, I'd use sed to change the whitespaces into newlines:
>
> cat file_with_multiple_words_per_line | sed -e 's/ /\n/g' | <grep exp>

Or use `fmt` with a very short line length target to force each word to
take up one line:

  fmt -1 file | grep foo


(I got into this habit when I started noticing that some versions of sed
don't seem to like having newline metacharacters in the pattern or sub.)



-- 
Chris Devers        cdevers at pobox.com
http://devers.homeip.net:8080/resume/

foolproof, adj.
(Of a system) inaccessible by the USER. Compare INTUITIVITY.

    -- from _The Computer Contradictionary_, Stan Kelly-Bootle, 1995



More information about the Discuss mailing list