简体   繁体   English

使用 sed 删除空行

[英]Delete empty lines using sed

I am trying to delete empty lines using sed:我正在尝试使用 sed 删除空行:

sed '/^$/d'

but I have no luck with it.但我没有运气。

For example, I have these lines:例如,我有这些行:

xxxxxx


yyyyyy


zzzzzz

and I want it to be like:我希望它像:

xxxxxx
yyyyyy
zzzzzz

What should be the code for this?这应该是什么代码?

You may have spaces or tabs in your "empty" line.您的“空”行中可能有空格或制表符。 Use POSIX classes with sed to remove all lines containing only whitespace:使用带有sedPOSIX 类来删除所有仅包含空格的行:

sed '/^[[:space:]]*$/d'

A shorter version that uses ERE, for example with gnu sed:使用 ERE 的较短版本,例如使用 gnu sed:

sed -r '/^\s*$/d'

(Note that sed does NOT support PCRE.) (注意 sed支持 PCRE。)

I am missing the awk solution:我错过了awk解决方案:

awk 'NF' file

Which would return:哪个会返回:

xxxxxx
yyyyyy
zzzzzz

How does this work?这是如何运作的? Since NF stands for "number of fields", those lines being empty have 0 fields, so that awk evaluates 0 to False and no line is printed;由于NF代表“字段数”,那些为空的行有 0 个字段,因此 awk 将 0 评估为 False 并且不打印任何行; however, if there is at least one field, the evaluation is True and makes awk perform its default action: print the current line.但是,如果至少有一个字段,则计算结果为 True 并使awk执行其默认操作:打印当前行。

sed '/^$/d' should be fine, are you expecting to modify the file in place? sed '/^$/d'应该没问题,您是否希望修改文件? If so you should use the -i flag.如果是这样,您应该使用-i标志。

Maybe those lines are not empty, so if that's the case, look at this question Remove empty lines from txtfiles, remove spaces from start and end of line I believe that's what you're trying to achieve.也许这些行不是空的,所以如果是这样的话,看看这个问题Remove empty lines from txtfiles, remove spaces from start and end of line我相信这就是你想要实现的。

I believe this is the easiest and fastest one:我相信这是最简单和最快的一个:

cat file.txt | grep .

If you need to ignore all white-space lines as well then try this:如果您还需要忽略所有空白行,请尝试以下操作:

cat file.txt | grep '\S'

Example:例子:

s="\
\
a\
 b\
\
Below is TAB:\
    \
Below is space:\
 \
c\
\
"; echo "$s" | grep . | wc -l; echo "$s" | grep '\S' | wc -l

outputs输出

7
5

With help from the accepted answer here and the accepted answer above, I have used:此处接受的答案和上面接受的答案的帮助下,我使用了:

$ sed 's/^ *//; s/ *$//; /^$/d; /^\s*$/d' file.txt > output.txt

`s/^ *//`  => left trim
`s/ *$//`  => right trim
`/^$/d`    => remove empty line
`/^\s*$/d` => delete lines which may contain white space

This covers all the bases and works perfectly for my needs.这涵盖了所有基础,非常适合我的需求。 Kudos to the original posters @Kent and @kev感谢原始海报@Kent 和@kev

Another option without sed , awk , perl , etc没有sedawkperl等的另一种选择

strings $file > $output

strings - print the strings of printable characters in files.字符串 - 打印文件中可打印的字符串。

你可以说:

sed -n '/ / p' filename    #there is a space between '//'

You are most likely seeing the unexpected behavior because your text file was created on Windows, so the end of line sequence is \r\n .您很可能会看到意外行为,因为您的文本文件是在 Windows 上创建的,因此行尾是\r\n You can use dos2unix to convert it to a UNIX style text file before running sed or use您可以在运行 sed 或使用之前使用 dos2unix 将其转换为 UNIX 样式的文本文件

sed -r "/^\r?$/d"

to remove blank lines whether or not the carriage return is there.删除空行,无论是否有回车符。

The command you are trying is correct, just use -E flag with it.您尝试的命令是正确的,只需使用 -E 标志即可。

sed -E '/^$/d'

-E flag makes sed catch extended regular expressions. -E 标志使 sed 捕获扩展的正则表达式。 More info here更多信息在这里

你也可以使用“grep”来做类似的事情:

egrep -v "^$" file.txt

This works in awk as well.这也适用于 awk。

awk '!/^$/' file
xxxxxx
yyyyyy
zzzzzz

My bash -specific answer is to recommend using perl substitution operator with the global pattern g flag for this, as follows:我的bash特定答案是建议为此使用带有全局模式g标志的perl替换运算符,如下所示:

$ perl -pe s'/^\n|^[\ ]*\n//g' $file
xxxxxx
yyyyyy
zzzzzz

This answer illustrates accounting for whether or not the empty lines have spaces in them ( [\ ]* ), as well as using |这个答案说明了考虑空行中是否有空格( [\ ]* ),以及使用| to separate multiple search terms/fields.分隔多个搜索词/字段。 Tested on macOS High Sierra and CentOS 6/7.在 macOS High Sierra 和 CentOS 6/7 上测试。

FYI, the OP's original code sed '/^$/d' $file works just fine in bash Terminal on macOS High Sierra and CentOS 6/7 Linux at a high-performance supercomputing cluster.仅供参考,OP 的原始代码sed '/^$/d' $file在高性能超级计算集群的 macOS High Sierra 和 CentOS 6/7 Linux 上的bash终端中运行良好。

If you want to use modern Rust tools, you can consider:如果你想使用现代的 Rust 工具,你可以考虑:

  • ripgrep : ripgrep
    • cat datafile | rg '.' line with spaces is considered non empty带空格的行被认为是非空的
    • cat datafile | rg '\S' cat datafile | rg '\S' line with spaces is considered empty cat datafile | rg '\S'带空格的行被认为是空的
    • rg '\S' datafile line with spaces is considered empty ( -N can be added to remove line numbers for on screen display) rg '\S' datafile行被认为是空的(可以添加-N以删除屏幕显示的行号)
  • sd sd
    • cat datafile | sd '^\n' '' cat datafile | sd '^\n' '' line with spaces is considered non empty cat datafile | sd '^\n' ''带空格的行被认为是非空的
    • cat datafile | sd '^\s*\n' '' cat datafile | sd '^\s*\n' '' line with spaces is considered empty cat datafile | sd '^\s*\n' ''带空格的行被认为是空的
    • sd '^\s*\n' '' datafile inplace edit sd '^\s*\n' '' datafile就地编辑

NF 是 awk 的命令,可用于删除文件awk NF filename中的空行,并使用 sed sed -r "/^\r?$/d"

For me with FreeBSD 10.1 with sed worked only this solution:对我来说,使用sed的 FreeBSD 10.1 仅适用于这个解决方案:

sed -e '/^[     ]*$/d' "testfile"

inside [] there are space and tab symbols. []里面有空格和制表符。

test file contains:测试文件包含:

fffffff next 1 tabline ffffffffffff

ffffffff next 1 Space line ffffffffffff

ffffffff empty 1 lines ffffffffffff

============ EOF =============

使用 vim 编辑器删除空行

:%s/^$\n//g

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM