Why does sed / ^ $ / d delete only empty lines, but / ^ $ / p prints all lines?

I can use sed /^$/d <file> to delete all empty lines in a file, but what if I want to print only empty lines? The sed /^$/p <file> command prints all lines in a file.

The reason I want to do this is because we use the EDA (Expedition) program, which uses a regular expression to run rules by network name. I am trying to find a way to search for all networks that have no assigned names. I thought using ^$ would work, but it just finishes searching for all the networks, which is what /^$/p does. So is there any other way to do this?

+4
source share
4 answers

Unless otherwise indicated, sed will print the template space when it finishes processing it. If you carefully look at your result, you will notice that you get two empty lines for each file. You will need to use the -n command line switch to stop sed from printing. A.

 sed -n /^$/p infile 

Should work the way you want.

+17
source

Sed prints each line by default, so the p flag is useless. To make this useful, you need to give sed the -n switch. In fact, it looks like you are doing what you want:

 sed -n /^$/p 
+2
source

You can also use grep like:

 grep '^$' infile 
+1
source

think differently, not p , but ! d

you can try:

 sed '/^$/!d' yourFile 
+1
source

Source: https://habr.com/ru/post/1380768/


All Articles