LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Discard short lines? (https://www.linuxquestions.org/questions/linux-newbie-8/discard-short-lines-803956/)

danielbmartin 04-24-2010 11:54 AM

Discard short lines?
 
Hello.

I have a file containing one word per line.
I want to discard all lines containing words shorter than five characters.

Have:
Once
upon
a
midnight
dreary
while
I
pondered
weak
and
weary

Want:
midnight
dreary
while
pondered
weary

Is there a Linux command which will do this?
I seek a basic command, not awk, not Perl, because I haven't learned those things yet.

Daniel B. Martin

amani 04-24-2010 12:14 PM

use regular expressions and any editing tool... what else?

pixellany 04-24-2010 12:22 PM

See my post in your other recent thread.....

Code:

egrep ".{5}.*" filename
Do you understand how this works? If not, go here: http://www.grymoire.com/Unix/Regular.html

H_TeXMeX_H 04-24-2010 12:23 PM

Code:

bash-3.1$ grep ..... test.txt
midnight
dreary
while
pondered
weary

I know I probably shouldn't have posted it, but it's so simple.

grail 04-24-2010 11:10 PM

Knowing how you are not ready for awk and such, here is asimple bash alternative:
Code:

#!/bin/bash

while read line
do
        [[ ${#line} -ge 5 ]] && echo $line
done <infile


danielbmartin 04-25-2010 09:11 PM

Quote:

Originally Posted by pixellany (Post 3946446)
See my post in your other recent thread.....

Code:

egrep ".{5}.*" filename
Do you understand how this works? If not, go here: http://www.grymoire.com/Unix/Regular.html

Thanks for a perfect answer.
I'll mark this one as solved.

Daniel B. Martin


All times are GMT -5. The time now is 09:14 AM.