remove “empty” lines from a text file shell script

不羁的心 提交于 2019-12-11 13:19:52

问题


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'


回答1:


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



回答2:


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. :-/



来源:https://stackoverflow.com/questions/33756062/remove-empty-lines-from-a-text-file-shell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!