sed N 和 P 多行命令选项未按预期工作

sed N 和 P 多行命令选项未按预期工作

数据样本:

output=$(cat <<EOF
This is line 1.
This is line 2.
This is line 3.
This is line 4.
This is line 5.
This is line 6.
EOF
)
echo "${output}" > ~/data8.txt

为什么下面两行返回相同的结果?

sed '{
N
3,$P
}' ~/data8.txt

This is line 1. 
This is line 2. 
This is line 3. 
This is line 3. 
This is line 4. 
This is line 5. 
This is line 5. 
This is line 6.


sed '{
N
4,$P
}' ~/data8.txt

我期望上面的行返回:

This is line 1.
This is line 2.
This is line 3.
This is line 4.
This is line 4.
This is line 5.
This is line 5.
This is line 6.

但它返回:

This is line 1.
This is line 2.
This is line 3.
This is line 3.
This is line 4.
This is line 5.
This is line 5.
This is line 6.

请您帮助解决这种奇怪的行为。

答案1

当读取第 3 行时,命令添加了第 4 行N,因此地址4,$匹配并且P执行命令,打印缓冲区中的第一行,即第 3 行。

3,$结果是相同的,因为第 3 行和第 4 行是一起读取的,因此地址是否为或并不重要4,$

相关内容