简体   繁体   English

Sed-从CSV文件中删除空白行

[英]Sed - Delete blank rows from csv file

I am trying to extract some .csv files from gz (compressed) files. 我正在尝试从gz(压缩)文件中提取一些.csv文件。 I want to delete the rows in the excel file that are blank. 我想删除Excel文件中空白的行。 I am using the code below but it does not work : 我正在使用下面的代码,但它不起作用:

$entoli = 'zcat ' . str_replace(' ', '\ ', $filePath) . ' | sed \'/^\s*$/d\'  > ' . str_replace(' ', '\ ', $savePath);
exec($entoli);

where $filePath is the directory of the original compressed and $savePath is the directory where the extracted file will be saved. 其中$filePath是原始压缩文件的目录,而$savePath是将保存提取文件的目录。 Also, I am using str_replace(' ', '\\ ', ....) just in case there are spaces in the path. 另外,我正在使用str_replace(' ', '\\ ', ....) ,以防路径中有空格。

I used this code before in order to delete the rows that do not start with the date format and it works : 我之前使用此代码是为了删除不以日期格式开头的行,并且可以使用:

$entoli = 'zcat ' . str_replace(' ', '\ ', $filePath) . ' | sed \'/^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/!d\'  > ' . str_replace(' ', '\ ', $savePath);
exec($entoli);

so, I presume that only this line has to be changed : 因此,我假设只需要更改此行:

sed \'/^\s*$/d\'

After trying to use different syntax, I managed to make it work : 在尝试使用其他语法之后,我设法使其工作:

$entoli = 'zcat ' . str_replace(' ', '\ ', $filePath) . ' | sed \'/^,,,,/d\'  > ' . str_replace(' ', '\ ', $temp);
exec($entoli);

It works with awk too: 它也可以与awk一起使用:

$entoli = 'zcat ' . str_replace(' ', '\ ', $filePath) . ' | awk \'!/^,,,,/\'  > ' . str_replace(' ', '\ ', $temp);

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

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