I'm able to use sed /^$/d <file>
to delete all the blank lines in the file, but what if I want to print all the blank lines only? The command sed /^$/p <file>
prints all the lines in file.
我可以使用sed / ^ $ / d
The reason I want to do this is that we use an EDA program (Expedition) that uses regex to run rules on the names of nets. I'm trying to find a way to search for all nets that don't have names assigned. I thought using ^$
would work, but it just ends up finding all nets, which is what /^$/p
is doing too. So is there a different way to do this?
我想这样做的原因是我们使用EDA程序(Expedition)使用正则表达式来运行网络名称的规则。我正试图找到一种方法来搜索没有分配名称的所有网络。我认为使用^ $会起作用,但它最终会找到所有网络,这也是/ ^ $ / p正在做的事情。那么有不同的方法吗?
4 个解决方案
#1
17
Unless otherwise specified sed will print the pattern space when it has finished processing it. If you look carefully at your output you'll notice that you get 2 blank lines for every one in the file. You'll have to use the -n command line switch to stop sed from printing.
除非另有说明,否则sed将在完成处理后打印图案空间。如果仔细查看输出,您会发现文件中的每一行都有2个空行。您必须使用-n命令行开关来停止打印sed。
sed -n /^$/p infile
Should work as you want.
应该按你的意愿工作。
#2
2
Sed prints every line by default, and so the p flag is useless. To make it useful, you need to give sed the -n switch. Indeed, the following appears to do what you want:
Sed默认打印每一行,因此p标志是无用的。为了使它有用,你需要给sed -n开关。事实上,以下似乎做你想要的:
sed -n /^$/p
#3
1
You can also use grep
as:
你也可以使用grep作为:
grep '^$' infile
#4
1
think in another way, don't p, but !d
以另一种方式思考,不要p,但是!d
you may try:
你可以尝试:
sed '/^$/!d' yourFile
#1
17
Unless otherwise specified sed will print the pattern space when it has finished processing it. If you look carefully at your output you'll notice that you get 2 blank lines for every one in the file. You'll have to use the -n command line switch to stop sed from printing.
除非另有说明,否则sed将在完成处理后打印图案空间。如果仔细查看输出,您会发现文件中的每一行都有2个空行。您必须使用-n命令行开关来停止打印sed。
sed -n /^$/p infile
Should work as you want.
应该按你的意愿工作。
#2
2
Sed prints every line by default, and so the p flag is useless. To make it useful, you need to give sed the -n switch. Indeed, the following appears to do what you want:
Sed默认打印每一行,因此p标志是无用的。为了使它有用,你需要给sed -n开关。事实上,以下似乎做你想要的:
sed -n /^$/p
#3
1
You can also use grep
as:
你也可以使用grep作为:
grep '^$' infile
#4
1
think in another way, don't p, but !d
以另一种方式思考,不要p,但是!d
you may try:
你可以尝试:
sed '/^$/!d' yourFile