简体   繁体   中英

Sed - Delete blank rows from csv file

I am trying to extract some .csv files from gz (compressed) files. I want to delete the rows in the excel file that are blank. 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. Also, I am using str_replace(' ', '\\ ', ....) just in case there are spaces in the path.

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:

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

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