The 'd' command in sed

From sed documentation:

d      Delete the pattern space; immediately start next cycle.

What does the next cycle mean ? I understand that sed will not use the following commands, which, after the d command , will begin to read the next line from the input stream and process it. But that doesn't seem to be true. See this exam:

[root@localhost ~]# cat -A test.txt
aaaaaaaaaaaaaa$
$
bbbbbbbbbbbbb$
$
$
ccccccccc$
ddd$
$
eeeeeee$
[root@localhost ~]# cat test.txt | sed '/^$/d;p;p'
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
bbbbbbbbbbbbb
bbbbbbbbbbbbb
bbbbbbbbbbbbb
ccccccccc
ccccccccc
ccccccccc
ddd
ddd
ddd
eeeeeee
eeeeeee
eeeeeee
[root@localhost ~]# 

If the next loop starts immediately , the p command will not output.

Can anyone help me explain this please? Thank.

+5
source share
2 answers

This means that sed will read the next line and start processing it.

script , . delete. , . , .

sed '/./d;p;p' test.txt # matches all non-empty lines

, , .

+7

a) :

sed '/^$/{d;p;p}' test.txt

aaaaaaaaaaaaaa
bbbbbbbbbbbbb
ccccccccc
ddd
eeeeeee

d : '/^ $/d; p; p'. 2 . "p'- ", . p , - , .

b) . ( )

+1

All Articles