# suppress automatic printing of pattern space # 关闭 pattern space 的自动打印,如果 command 中已经指定了 p, 可能就需要指定该选项 -n, --quiet, --silent
script command
1 2 3 4 5 6 7 8 9 10 11 12 13
d # Delete pattern space. Start next cycle.
# Copy 即覆盖, append 即追加 h H # Copy/append pattern space to hold space. g G # Copy/append hold space to pattern space. x # Exchange the contents of the hold and pattern spaces.
n N # Read/append the next line of input into the pattern space. p # Print the current pattern space. P # Print up to the first embedded newline of the current pattern space.
w filename # Write the current pattern space to filename. W filename # Write the first line of the current pattern space to filename. This is a GNU extension.
examples
逆序打印文件行,等同于 tac
1 2 3 4 5 6 7 8 9
# 第一行,只将 pattern space 中的内容 copy 到 hold space,再删除 pattern space,下一轮循环 # 接下来,每一行,将 hold space 的内容 append 到 pattern space,再删除 pattern space,下一轮循环 sed '1{h;d};G;h;$!d' seq.txt
sed '1!G;h;$!d' t.txt # 1!G —— 只有第一行不执行G命令,将hold space中的内容append回到pattern space # h —— 第一行都执行h命令,将pattern space中的内容拷贝到hold space中 # $!d —— 除了最后一行不执行d命令,其它行都执行d命令,删除当前行