Delete empty rows in CSV file with sed

Posted on 23. August 2017 Comments

This is for when you have empty lines (so lot’s of ,,,,,,) in your file.

MacOS

sed -i '' -e '/^,*$/d' filename.csv
find ./ -type f -exec sed -i '' -e '/^,*$/d' {} \;

Linux

sed -i '/^,*$/d' filename.csv
find ./ -type f -exec sed -i '/^,*$/d' {} \;

 

^ marks the beginning of a line
,* marks a possible infinite amount of commas
$ marks the end of a line

Comments

  1. Windows PowerShell:
    (get-content filename.csv | select-string -pattern ‚^;*$‘ -notmatch | Out-String).Trim() | Out-File filename.csv

    The conversion to a string together with Trim is needed, because select-string formats the output with a blank line at the start and the end.

    It may be interesting to mention here, that the ‚$‘ operator matches end of line depending on the system. On windows this may be carriage return + line feed, while Linux uses only LF only and Mac used to have CR and now only LF like Linux.
    On modern systems the regex ‚\r?\n‘ should detect the end of line with common files.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.