Grep Text File for Lines Containing 1 or 2 Words

B

Buck Turgidson

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.
 
A

A. Sinan Unur

[ top-posting fixed. don't do that ]
awk 'NF && NF <= 2' FILENAME

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

Still, see

perldoc a2p

Sinan.
 
W

Wes Groleau

Buck said:
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.
 
A

A. Sinan Unur

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
 
J

John W. Krahn

Buck said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top