Datacrawler
Datacrawler

Reputation: 2876

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\'

Upvotes: 0

Views: 131

Answers (1)

Datacrawler
Datacrawler

Reputation: 2876

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);

Upvotes: 1

Related Questions