Delete empty lines in csv file
Asked Answered
D

4

8

I have a file with 4 million of lines, every line ends with the char $, but I mistakenly add a new line after the the line delimiter while scraping a website, so right now it is looking like this:

fist name, last name, phone, address, postal code, city, region,$

$

fist name, last name, phone, address, postal code, city, region,$

$

the new line '$' only shows up of course if I use :set list, but I'm trying to use this file for a bulk insert in mysql and I'm having problems with it now.

I would like to change the file to:

fist name, last name, phone, address, postal code, city, region,$

fist name, last name, phone, address, postal code, city, region,$

How can I do this? with sed or awk or even vi ? looked up around and what I found is not really applying to this case.

please don't take in consideration the extra empty line shown above.

Thanks in advance

Duckbill answered 13/3, 2014 at 0:16 Comment(0)
G
12

To remove blank lines with sed:

sed -i '/^$/d' yourfile.csv

To remove lines consisting of a single $:

sed -i '/^$$/d' yourfile.csv

Most versions of sed support the -i switch; if yours does not you will need e.g. sed '/^$$/d' yourfile.csv > newfile.csv.

Removing blank lines with white space is more complicated. This usually works:

sed '/^ *$/d' yourfile.csv

If this is not sufficient, try checking also for tabs. For older sed's, this will work:

sed '/^[ X]*$/d' yourfile.csv

where X here a tab, entered via Control-V Tab.

Newer sed's will take a [ \t\r]* or \s* or [[:space:]]*, sometimes requiring a -E switch.

Grayling answered 13/3, 2014 at 2:20 Comment(0)
A
2

grep can filter lines by match (or negative match) against a regex. To exclude empty lines:

grep -v '^$' yourfile.csv > yourfile_fixed.csv
Adaptable answered 13/3, 2014 at 0:20 Comment(1)
Thanks for the reply, I believe this should work! But I actually just found that only the first 100 lines are like this because I had to do some copy and paste with these first 100 entries.Duckbill
A
1

Here are your options:

With awk:

awk 'NF' file > tmp && mv tmp file

With sed (in-place changes so make sure to backup your file using -i.bak):

sed -i '/^$/d' file

With vi:

:g/^$/d
Ambidextrous answered 13/3, 2014 at 2:6 Comment(2)
Just loved awk 'NF' approach!Flinders
How does the NF number-of-fields work in your example?Immateriality
P
0

A blank line of a CSV file format is a comma delimiter in Linux; therefore you can delete it with the below command

sed '/^,/d' file
Penny answered 29/8, 2024 at 21:0 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.