How to remove lines with less than 10 words from a text file?

1 replies
  • OFF TOPIC
  • |
I didn't know where else to ask this, but I'm trying to remove all lines with less than 10 words from a text file. I've downloaded Notepad++, Notepad2, VIM & jEdit but still haven't figured out the expression.
  • Profile picture of the author mojojuju
    Here's a pattern to search for:

    ^(\w*\b\s?){0,9}$

    I'm not sure what regex features the programs you mentioned have, but if you want to use perl, here's a one liner to do the replacements you need:

    perl -pi -e 's/^(\w*\b\s?){0,9}$//g' file.txt

    edit: Actually, I think it would be more accurate to go with:

    ^([^\s]*\s?){0,9}$ as your pattern because the previous one doesn't acknowledge punctuation (the \w from above only looks for words, numbers, and maybe hypens and underscores). It depends on what you're looking for, but you might want to give that second one a try.
    Signature

    :)

    {{ DiscussionBoard.errors[5158923].message }}

Trending Topics