sed使用(mac版)

42 篇文章 0 订阅
11 篇文章 1 订阅

1.增

#方式一,在第一行插入字符串helloworld0。vim里,注意1i\,后边有一个空格
sed -i "" "1i\ 
> hellworld0
> " test.txt

#方式二,在第一行插入helloworld。注意 $'\n' 。
sed -i "" "1i\\"$'\n'"helloworld" test.txt
sed -i "" '1i\'$'\n''helloworld' test.txt

#在一行之后,追加字符串。(新增内容显示在第二行)
sed -i "" "1a\\"$'\n'"helloworld" test.txt

2.删

#直接删除test.txt的第一行(d表示delete)
sed -i "" "1d" test.txt        

#直接删除2-5行的内容       
sed -i "" "2,5d" test.txt

#直接删除test.txt中world8所在行
sed -i "" "/world8/d” test.txt     

#直接删除test.txt中正则匹配的字符串所在行
sed -i "" "/world.*/d” test.txt    

3.查

#打印第一行。p表示打印。
sed -n "1p" test.txt

#打印第一到三行。
sed -n "1,3p" test.txt

#打印包含 “beijing” 字符串所在的行。
sed -n "/beijing/p" test.txt

4.改

#把world1改成world0。s表示替换,2表示第二个world1,g表示全局。
sed -i "" "s/world1/world0/2" test.txt

#把第二行,第三行的所有hello替换为welcome
sed -i "" "2,3s/hello/welcome/" test.txt

#找到world3所在行,把所有的hello替换为welcome
sed -i "" "/world3/s/hello/welcome/g" test.txt

#正则匹配修改。把所有的 world+一个数字 改成了 beijing。
sed -i "" "s/world[0-9]/beijing/g" test.txt

#变量。\(.*\)会匹配出一个变量。\1表示第一个变量,\2表示第二个变量。
sed -i "" "s/\(.*\)-\(.*\)/\2-\1/g" test.txt

#把test.txt的2-5行替换为字符串
sed -i "" '2,5c\'$'\n''China' test.txt

5.读取,写入数据

#把test.txt的第一行替换为test1.txt的内容。
sed -i "" "1r test1.txt" test.txt

#把test1.txt的第三,四行数据,写入到test.txt。
sed -i "" "3,4w test.txt" test1.txt 
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值