bash 删除csv文件中的空行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22366622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:53:24  来源:igfitidea点击:

Delete empty lines in csv file

linuxbashsedawkvi

提问by user3344414

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:

我有一个包含 400 万行的文件,每一行都以 char $ 结尾,但是我在抓取网站时错误地在行分隔符后添加了一个新行,所以现在它看起来像这样:

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.

当然,如果我使用 :set list,新行 '$' 只会出现,但我正在尝试使用此文件在 mysql 中进行批量插入,但我现在遇到了问题。

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.

我怎样才能做到这一点?使用 sed 或 awk 甚至 vi ?环顾四周,我发现的内容并不真正适用于这种情况。

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

请不要考虑上面显示的额外空行。

Thanks in advance

提前致谢

回答by Joseph Quinsey

To remove blank lines with sed:

删除空行sed

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

To remove lines consisting of a single $:

要删除由单个 组成的行$

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

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

大多数版本的 sed 都支持-iswitch;如果你的没有,你将需要例如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,这将起作用:

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

where Xhere a tab, entered via Control-VTab.

其中,X在此选项卡,通过进入Control-VTab

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

较新的 sed 将采用[ \t\r]*or\s*[[:space:]]*,有时需要-E切换。

回答by that other guy

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

grep可以通过匹配(或否定匹配)对正则表达式过滤行。排除空行:

grep -v '^$' yourfile.csv > yourfile_fixed.csv

回答by jaypal singh

Here are your options:

以下是您的选择:

With awk:

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.bak):

sed -i '/^$/d' file

With vi:

vi

:g/^$/d