打印文本内容并输出行号
实例
打印文本内容,输出行号,并且删除2-5行内容
nl regular_express.txt | sed '2,5d'
删除第2行
nl regular_express.txt | sed '2d'
删除第三行到最后一行, $定位到最后一行
nl regular_express.txt | sed '3,$d'
在原文件中删除第 1 行:
sed -i '1d' regular_express.txt
在行前(i)行后(a)添加字符串
实例:
在第二行前添加 test 字符串
nl regular_express.txt | sed '2i test'
在第二行后添加 test 字符串
nl regular_express.txt | sed '2a test'
在第二行后加入两行 test,“\n”表示换行符
nl regular_express.txt | sed '2a test\ntest'
替换内容选项c
实例:
将2-5行内容取代为No 2-5 number
nl regular_express.txt | sed '2,5c No 2-5 number'
列出制定行的内容
sed 命令中-n 为安静模式选项。以下两条命令执行结束后可对比结果。
nl regular_express.txt |sed -n '5,7p'
nl regular_express.txt |sed '5,7p'
wm.png
注: sed 是 sed -e 的简写, 后接单引号