Search code examples
fileunixsedline

Remove lines from a text file


I am trying to remove the lines having only 2 words or less than that. my text file is like this

1.just be patient.
2.posters?
3.I have got two jobs, but they're not for you.
4.what?
5.I am.

Which should give me output...

1.just be patient.
2.I have got two jobs, but they're not for you.

Can anyone out there suggest me the best way to do it... (using sed or kind of things)

Thanks in advance


Solution

  • This should be an easy task with awk in case you are ok with it.

    awk 'NF>2' Input_file
    

    Explanation:

    • Checking condition here NF>2 if NF(number of fields, default variable in awk) value is greater than 2.

    • awk has by default space as a delimiter so as per OP it should be more than 2 words hence given >2 condition.

    • Now comes part how lines getting printed which are satisfying the condition? awk works on method of condition and action, if a condition is TRUE and no action is mentioned(like this code) then by default print of current line will happen.