Grep Text File for Lines Containing 1 or 2 Words

Discussion in 'Perl Misc' started by Buck Turgidson, Jan 25, 2005.

  1. Looking for help in writing a 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.
     
    Buck Turgidson, Jan 25, 2005
    #1
    1. Advertising

  2. "Buck Turgidson" <> wrote in
    news:m_CJd.95397$Jk5.73982@lakeread01:

    [ top-posting fixed. don't do that ]

    > "Tad McClellan" <> wrote in message
    > news:...
    >> Buck Turgidson <> wrote:
    >> > Looking for help in writing a 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.

    >>
    >>
    >> Show us the code you have so far, and we will help you fix it.

    >
    > awk 'NF && NF <= 2' FILENAME
    >


    You seem not to realize that this group is about Perl.

    Still, see

    perldoc a2p

    Sinan.
     
    A. Sinan Unur, Jan 26, 2005
    #2
    1. Advertising

  3. Buck Turgidson

    Wes Groleau Guest

    Buck Turgidson wrote:
    > Looking for help in writing a 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.


    print if (/^\W*\w+\W+\w+\W*$/);

    Did I get that right? I'm not an expert.

    Or you could write something just as short in awk, and
    use 'a2p' to convert it into a page of unreadable perl.

    Come to think of it, do you have to use perl?

    perl is great, but it's not all there is to life.
    Even egrep can do what you asked.

    --
    Wes Groleau
    Expert, n. Someone who comes from out of town and shows slides.
     
    Wes Groleau, Jan 26, 2005
    #3
  4. A. Sinan Unur <> wrote:

    > see
    >
    > perldoc a2p



    Make that:

    man a2p


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 26, 2005
    #4
  5. Tad McClellan <> wrote in
    news::

    > A. Sinan Unur <> wrote:
    >
    >> see
    >>
    >> perldoc a2p

    >
    >
    > Make that:
    >
    > man a2p


    Well, there seem to be some systems where the former works:

    FreeBSD 5.2.1-RELEASE (RECEX) #1:
    asu1@recex:~ > perldoc a2p

    A2P(1) User Contributed Perl Documentation A2P(1)

    NAME
    a2p - Awk to Perl translator

    perldoc a2p works with ActiveState Perl as well. Those systems are
    unlikely to have man.

    Just thought I would point it out.

    Sinan
     
    A. Sinan Unur, Jan 26, 2005
    #5
  6. Buck Turgidson

    Guest

    terry l. ridder wrote:
    > On Tue, 25 Jan 2005, Buck Turgidson wrote:
    >


    > is iixx a "word" or a number ( roman numerial )?


    Not to pick nits, but iixx is not a roman numerial (18 would be xviii).
     
    , Jan 26, 2005
    #6
  7. Buck Turgidson wrote:
    > Looking for help in writing a 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.


    perl -ane'@F<3&&print' yourfile.txt


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Jan 26, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sahm
    Replies:
    4
    Views:
    42,634
    rel0aded911
    Nov 23, 2009
  2. Murali
    Replies:
    2
    Views:
    573
    Jerry Coffin
    Mar 9, 2006
  3. Umesh
    Replies:
    16
    Views:
    711
    blufox
    Jun 5, 2007
  4. Umesh
    Replies:
    8
    Views:
    463
    Charlton Wilbur
    May 30, 2007
  5. User
    Replies:
    5
    Views:
    469
    Tad McClellan
    Jun 13, 2004
Loading...

Share This Page