简体   繁体   中英

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'

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM