Linux中的sed命令,使用方法之一「替换字符串中的内容 」,以及「s/ / / 」和「s/ / /g」之间的区别

■前言

按照如下写法,可以实现替换字符串的功能

 

■具体操作

sed 's/hello/hi/' 321.html

替换对象 hello

替换后,显示的内容 hi

只是在输出的时候,被替换,不会改变原有的文件的内

--

 

 

■应用:

虽然,只是临时改变(不会改变原有文件中的内容),

但是,在shell脚本中,是我们可以把改变后的值,作为变量保存起来,在后续处理中使用

 

■区别:

s/ / /   :匹配第一个

s/ / /g   :匹配所有

sxz001@sxzap01:~/temp$ echo hello hello world | sed 's/hello/hi/'
hi hello world
sxz001@sxzap01:~/temp$ echo hello hello world | sed 's/hello/hi/g'
hi hi world
sxz001@sxzap01:~/temp$

 

■更多操作

https://blog.csdn.net/qq_37931597/article/details/86505723

 

■帮助文档

man sed

---

SED(1)                           User Commands                          SED(1)

NAME
       sed - stream editor for filtering and transforming text

SYNOPSIS
       sed [OPTION]... {script-only-if-no-other-script} [input-file]...

DESCRIPTION
       Sed  is a stream editor.  A stream editor is used to perform basic text
       transformations on an input stream (a file or input from  a  pipeline).
       While  in  some  ways similar to an editor which permits scripted edits
       (such as ed), sed works by making only one pass over the input(s),  and
       is consequently more efficient.  But it is sed's ability to filter text
       in a pipeline which particularly distinguishes it from other  types  of
       editors.

       -n, --quiet, --silent

              suppress automatic printing of pattern space

       -e script, --expression=script

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值