简体   繁体   English

从文本文件外壳脚本中删除“空”行

[英]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? 我对shell脚本还很陌生,我该怎么做呢?

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: 由于NF存储此值,因此您可以简单地说:

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. 只是为了好玩,我在开始使用OP时使用了sed ,并保持了解决方案格式的详细,以防在选择要删除的行时日志文件的确切布局起作用。 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. 正则表达式在这里查找行的开头,后跟0个或多个数字,后跟一个空格,然后是零个或多个数字,然后是一个文字周期,后跟零个或多个数字,零个或多个空格,然后是结尾行并将其删除。

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. 有趣的是,在Ubuntu上的bash版本上,我无法使用\\ d的缩写来表示数字,而不能使用加号来表示一个或多个数字。 Looks like it's time to do some updating. 看起来是时候进行一些更新了。 :-/ :-/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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