samsamara
samsamara

Reputation: 4750

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'

Upvotes: 1

Views: 493

Answers (2)

Gary_W
Gary_W

Reputation: 10360

Just for the fun of it I used sed as the OP started with and kept the solution formatting detailed in case the exact layout of the log file may come into play when selecting the lines to delete. Here the regex looks for the beginning of the line, followed by 0 or more numbers, followed by a space, followed by zero or more numbers, followed by a literal period followed by zero or more numbers, zero or more spaces then the end of the line and removes them.

sed '/^[0-9]* [0-9]*\.[0-9]* *$/d' x.log > x_modded.log

Interesting that on my version of bash on Ubuntu I was unable to use the shorthand of \d for a digit and the plus sign for one or more numbers. Looks like it's time to do some updating. :-/

Upvotes: 1

fedorqui
fedorqui

Reputation: 289495

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

Upvotes: 6

Related Questions