Linux学习20190313
sed
sed命令详解
sed:Stream Editor文本流编辑,sed是一个“非交互式的”面向字符流的编辑器。能同时处理多个文件多行的内容,可以不对原文件改动,把整个文件输入到屏幕,可以把只匹配到模式的内容输入到屏幕上。还可以对原文件改动,但是不会再屏幕上返回结果。
sed命令的语法格式:
sed的命令格式: sed [option] 'sed command’filename
sed的脚本格式:sed [option] -f 'sed script’filename
sed命令的选项(option):
-n :只打印模式匹配的行
-e :直接在命令行模式上进行sed动作编辑,此为默认选项
-f :将sed的动作写在一个文件内,用–f filename 执行filename内的sed动作
-r :支持扩展表达式
-i :直接修改文件内容
sed -n '5’p test.txt
sed -n '1,5’p test.txt
sed -n '1,′ptest.txtsed−n′/root/′ptest.txtsed−n′/1/′ptest.txtsed−n′in 'p test.txtsed -n '/root/'p test.txtsed -n '/^1/'p test.txtsed -n 'in
′
ptest.txtsed−n
′
/root/
′
ptest.txtsed−n
′
/
1
/
′
ptest.txtsed−n
′
in’p test.txt
sed -n '/r…o/'p test.txt
sed -n 'oo*'p test.txt
sed -e '1’p -e '/111/'p -n test.txt
sed '1’d test.txt
sed '1,3’d test.txt
sed '/oot/'d test.txt
sed ‘1,2s/ot/to/g’ test.txt
sed ‘s#ot#to#g’ test.txt
sed ‘s/[0-9]//g’ test.txt
sed ‘s/[a-zA-Z]//g’ test.txt
sed -r ‘s/(rot)(.)(bash)/\3\2\1/’ test.txt
sed 's/^.$/123&/’ test.txt
sed -i ‘s/ot/to/g’ test.txt
sed的匹配模式支持正则表达式
sed命令实现对文件内容的替换