【sed】常用命令

替换

替换某一整行

sed '1c hello' test   #将第一行替换为hello

str1替换为str2

sed  's/^str1.*/str2/'  filename  #以str1开头
sed  's/.*str1$/str2/'  filename   #以str1结尾
sed  's/.*str1.*/str2/'  filename  #含有str1

将含有xhn的行中的str1替换为str2

sed  ‘/xhn/s/str1/str2/g’ test

将1-5行替换为两行:

hello

world

 sed '1,5c hello
world' test

删除

删除空行

sed '/^ *$/d' test #只能删除没有特殊字符的空行
sed '/[[:space:]]*/d' test #可删除特殊字符
特别需要说明一下,linux中的^M是ctrl+v和ctrl+m打出来的
原文地址:https://www.cnblogs.com/xhnxhnu/p/9408704.html