Linux 使用sed删除空行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16414410/
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
Delete empty lines using sed
提问by jonas
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?
这应该是什么代码?
采纳答案by Kent
You may have spaces or tabs in your "empty" line. Use POSIX classeswith sed
to remove all lines containing only whitespace:
您的“空”行中可能有空格或制表符。使用POSIX 类withsed
删除所有仅包含空格的行:
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 NOTsupport PCRE.)
(请注意,sed的确实不支持PCRE)。
回答by Alberto Zaccagni
sed '/^$/d'
should be fine, are you expecting to modify the file in place? If so you should use the -i
flag.
sed '/^$/d'
应该没问题,您是否希望就地修改文件?如果是这样,您应该使用该-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 lineI believe that's what you're trying to achieve.
也许这些行不是空的,所以如果是这样的话,看看这个问题Remove empty lines from txtfiles, remove space from start and end of line我相信这就是你想要实现的。
回答by tank
You can say:
你可以说:
sed -n '/ / p' filename #there is a space between '//'
回答by ConMan
With help from the accepted answer hereand 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 的原始海报
回答by Lowbit
You can do something like that using "grep", too:
你也可以使用“grep”来做类似的事情:
egrep -v "^$" file.txt
回答by Vadim
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
回答by fedorqui 'SO stop harming'
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 fiedls, so that awk evaluates 0 to False and no line is printed; however, if there is at least one field, the evaluation is True and makes awk
perform its default action: print the current line.
这是如何运作的?由于NF
代表“字段数”,那些为空的行有 0 个字段,因此 awk 将 0 评估为 False 并且不打印任何行;但是,如果至少有一个字段,则评估为 True 并awk
执行其默认操作:打印当前行。
回答by Oleg Mazko
sed
sed
grep
格雷普
awk
awk
回答by Claes Wikner
This works in awk as well.
这也适用于 awk。
awk '!/^$/' file
xxxxxx
yyyyyy
zzzzzz
回答by FauChristian
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
. You can use dos2unix to convert it to a UNIX style text file before running sed or use
您很可能会看到意外行为,因为您的文本文件是在 Windows 上创建的,因此行尾序列是\r\n
. 您可以在运行 sed 或使用之前使用 dos2unix 将其转换为 UNIX 样式的文本文件
sed -r "/^\r?$/d"
to remove blank lines whether or not the carriage return is there.
删除空行,无论是否有回车。