Search code examples
linuxshelltext-processingblank-line

remove "empty" lines from a text file shell script


I have a set of output files and some are as below:

133 0.00295 nurse merit respect muslim 
134 0.00292 high dangerous reassure full 
135 0.00048 
136 0.0039  experience darren  
137 0.00097 _ _param_ui_control_scripts_save _param_pattern_value 
138 0.00292 find director

And I want to get the following file:

133 0.00295 nurse merit respect muslim 
134 0.00292 high dangerous reassure full 
136 0.0039  experience darren  
137 0.00097 _ _param_ui_control_scripts_save _param_pattern_value 
138 0.00292 find director

Just want to remove that particular line if it doesn't have anything after the second column. how can I do this I'm quite new to shell scripting?

probably a modification of this command? sed '/^$/d'


Solution

  • If the columns are space separated, what about checking that lines have more than two fields? Since NF stores this value, you can simply say:

    awk 'NF>2' file
    

    For your given input it returns:

    133 0.00295 nurse merit respect muslim 
    134 0.00292 high dangerous reassure full 
    136 0.0039  experience darren  
    137 0.00097 _ _param_ui_control_scripts_save _param_pattern_value 
    138 0.00292 find director