linux sed简单使用

sed 帮助信息:

-bash-4.1$ sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  --follow-symlinks
                 follow symlinks when processing in place; hard links
                 will still be broken.
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if extension supplied).
                 The default operation mode is to break symbolic and hard links.
                 This can be changed with --follow-symlinks and --copy.
  -c, --copy
                 use copy instead of rename when shuffling files in -i mode.
                 While this will avoid breaking links (symbolic or hard), the
                 resulting editing operation is not atomic.  This is rarely
                 the desired mode; --follow-symlinks is usually enough, and
                 it is both faster and more secure.
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -r, --regexp-extended
                 use extended regular expressions in the script.
  -s, --separate
                 consider files as separate rather than as a single continuous
                 long stream.
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
      --help     display this help and exit
      --version  output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-gnu-utils@gnu.org>.
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.

替换文件内容:

源文件内容:

supplier.size=100
pic.size=100
httpconnect.timeout=20000
httpclient.maxConnections=20
retryTimes=1

job.initial-delay=0
job.fixed-delay=60000

替换后输出:

-bash-4.1$ sed 's/supplier.size=100/supplier.size=200/g' task.properties 
supplier.size=200
pic.size=100
httpconnect.timeout=20000
httpclient.maxConnections=20
retryTimes=1

job.initial-delay=0
job.fixed-delay=60000

这时sed并没有实际修改源文件内容,只是动态的修改了内容中源文件内容并打印出来,如何想修改源文件内容并保存需要使用 -i 参数:

sed -i 's/supplier.size=100/supplier.size=200/g' task.properties 

修改指定行的内容:

-bash-4.1$ sed '7,8s/job/JOB/g' task.properties
supplier.size=100
pic.size=100
httpconnect.timeout=20000
httpclient.maxConnections=20
retryTimes=1

JOB.initial-delay=0
JOB.fixed-delay=60000


一次替换多个内容:

-bash-4.1$ sed -e 's/supplier.size=100/supplier.size=200/g' -e 's/pic.size=100/pic.size=200/g' task.properties 
supplier.size=200
pic.size=200
httpconnect.timeout=20000
httpclient.maxConnections=20
retryTimes=1

job.initial-delay=0
job.fixed-delay=60000

删除行:

删除1-2行:

-bash-4.1$ sed '1,2d' task.properties 
httpconnect.timeout=20000
httpclient.maxConnections=20
retryTimes=1

job.initial-delay=0
job.fixed-delay=60000


删除指定行:

-bash-4.1$ sed '2d' task.properties 
supplier.size=100
httpconnect.timeout=20000
httpclient.maxConnections=20
retryTimes=1

job.initial-delay=0
job.fixed-delay=60000

显示指定行:

-bash-4.1$ sed -n '1,2p' task.properties 
supplier.size=100
pic.size=100


替换指定内容之间的部分:

-bash-4.1$ sed '/supplier.size/,/retryTimes/s/pic.size=100/pic.size=200/g' task.properties 
supplier.size=100
pic.size=200
httpconnect.timeout=20000
httpclient.maxConnections=20
retryTimes=1

更多介绍请参考:http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856901.html


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值