Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Grep Text File for Lines Containing 1 or 2 Words

Reply
Thread Tools

Grep Text File for Lines Containing 1 or 2 Words

 
 
Buck Turgidson
Guest
Posts: n/a
 
      01-25-2005
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.



 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      01-26-2005
"Buck Turgidson" <(E-Mail Removed)> wrote in
news:m_CJd.95397$Jk5.73982@lakeread01:

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

> "Tad McClellan" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Buck Turgidson <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Wes Groleau
Guest
Posts: n/a
 
      01-26-2005
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.
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-26-2005
A. Sinan Unur <(E-Mail Removed)> wrote:

> see
>
> perldoc a2p



Make that:

man a2p


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      01-26-2005
Tad McClellan <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> A. Sinan Unur <(E-Mail Removed)> 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
 
Reply With Quote
 
takarov2003@yahoo.com
Guest
Posts: n/a
 
      01-26-2005

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

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      01-26-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reading numbers of words and lines in text file in C++ sahm C++ 4 11-23-2009 07:43 PM
How to extract words containing 'ab' & 'cd' in a text file? Umesh C Programming 16 06-05-2007 12:53 PM
how to remove lines containing 'abc' in a text file? Umesh C Programming 8 05-30-2007 05:25 PM
To delete few lines and add few lines at the end of a text file using c program Murali C++ 2 03-09-2006 04:45 PM
Grep Pattern, matching any two consecutive words having 3 to 8 chars each User Perl Misc 5 06-13-2004 12:14 PM



Advertisments