sed常识

文章目录


Notice:有的docker中可能没装vim/vi,但你又希望修改文件(修改处不多),可以使用 sed临时救命

sed - stream editor for filtering and transforming text
本质是个 编辑器!用于文本的增删改查

e.g. 打印一个文件的10 到20 行
sed -n '10,20'p b.txt

  • -n, --quiet, --silent
    suppress automatic printing of pattern space (取消了默认打印)
  • -p, Print the current pattern space.

e.g. 将末行输出
sed -n '$'p b.txt

  • ‘$’ ,means the end line

e.g.只打印第10行
sed -n '10'p b.txt

e.g. 文本输出替换 【这个最实用】

sed "s#for#just for#g" a.txt

在输出 a.txt 的内容时,将其中的for 替换成 just for ,但不改变原来文本的内容 【相当于 dry-run

sed -i "s#steel#iron steel #g" a.txt

直接将文本中的内容steel 替换成 iron steel

  • s: 正则表达式开始(通俗说就是 查找并替换
    s/regexp/replacement/
    Attempt to match regexp against the pattern space. If successful, replace that portion matched with replacement. The replacement may contain the special character & to refer to that portion of the pattern space which matched, and the special escapes \1 through \9 to refer to the corresponding matching sub-expressions in the regexp.
  • g :全局 global
  • -i : edit files in place
  • sg: 全局查找并替换
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值