[SOLVED] Elimination of lines having fewer than 3 words
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
$ cat infile
how now
now is the time for
now
holy cow
the quick brown fox
jumped over
the candlestick
$ egrep '(\w+ +){3}' infile
now is the time for
the quick brown fox
Well, as Firefox developers say, this is embarrassing.. There should be '*' (a.k.a. Kleene star -- zero or more) instead of '+' (one or more):
Code:
egrep '(\w+ *){3,}'
This regular expression match a string consisting of three or more words, each followed by zero or more spaces, that is how a three-or-more-words string looks like.
Previous attempt (with ' +') worked on your sample data because there were no line with exactly 3 words. If that would be the case, then there must be at least one space after last word for that RE to work:
Code:
$ echo 'a b c' | egrep '(\w+ +){3}'
$ echo 'a b c ' | egrep '(\w+ +){3}'
a b c
$ echo 'a b c ' | egrep '(\w+ *){3}'
a b c
$ echo 'a b c' | egrep '(\w+ *){3}'
a b c
Well, as Firefox developers say, this is embarrassing.. There should be '*' (a.k.a. Kleene star -- zero or more) instead of '+' (one or more):
Code:
egrep '(\w+ *){3,}'
This regular expression match a string consisting of three or more words, each followed by zero or more spaces, that is how a three-or-more-words string looks like.
Previous attempt (with ' +') worked on your sample data because there were no line with exactly 3 words. If that would be the case, then there must be at least one space after last word for that RE to work:
Code:
$ echo 'a b c' | egrep '(\w+ +){3}'
$ echo 'a b c ' | egrep '(\w+ +){3}'
a b c
$ echo 'a b c ' | egrep '(\w+ *){3}'
a b c
$ echo 'a b c' | egrep '(\w+ *){3}'
a b c
Note last space after 'c'.
EDIT: grail beats me again
The first code line fails but the code in the examples is different, and it works. Is there a t7po?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.