Re: Grep For Line Containing 1or 2 Words

From: Chris F.A. Johnson (cfajohnson_at_gmail.com)
Date: 01/26/05


Date: 26 Jan 2005 00:58:25 GMT

On Tue, 25 Jan 2005 at 21:47 GMT, Buck Turgidson wrote:
> Looking for help in writing a grep (or perl) that will pull from a text file
> those lines containing one or two "words". I want to reject any line that
> contains more than 2 words (a sentence).
>
> Appreciate any help from text processing experts.

    To get all lines with no more than 2 words:

awk 'NF <= 2' FILENAME

    To eliminate empty lines also:

awk 'NF && NF <= 2' FILENAME

-- 
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License


Relevant Pages