Grep is my enemy...

Spadge Fromley spadge at
Wed Oct 4 10:51:43 BST 2006

> Yes, this old chestnut again... sorry!
> I have a pattern file with keywords/phrases, 1 per line, and now and
> there there are blank lines scattered through the file.
> I want to filter the patterns on the fly (getting rid of the blank
> lines), and using the result as the pattern file for grep -f.
> I also want to supply a string for the grep data, not a file.  So my
> command looks something like this:
>     grep -i -v '^[     ]*$' <pattern_file> | grep -f <stdin> <string>
> Obviously, grep doesn't like the <string> part and complains bitterly
> that it can't find the file.
> Can this be done in one statement?  I'd like to keep it on one statement
> because it will be part of a procmail recipe, and I don't want to keep
> writing temporary files to disk.

I can't help but think this is probably best done using grep/sed (and
possibly awk if you're brave), or something.

Without my unix handbook to refer to, I suck at regex, but basically
something like: "fgrep $STRING /path/to/file | sed 's/^\ //g'"

{search 'file' for $STRING, replace all lines starting with ' ' with nothing}

Or if the blank lines aren't matched by the ' ' char, then ^[^A-Za-z0-9]
should do the trick, or something. Also, you could use sed on its own I
believe by doing "sed /$string/s/^[^A-Za-z0-9]//g file", but I'm already
getting out of my depth.

Good luck.


More information about the Ukfreebsd mailing list