9

关于sed命令当中包含变量的坑

 3 years ago
source link: http://www.eryajf.net/2268.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
关于sed命令当中包含变量的坑 |坐而言不如起而行! 二丫讲梵
> 迎刃而解 > 关于sed命令当中包含变量的坑
本文预计阅读时间 1 分钟

今天在写脚本时用到了sed命令,其中也学到不少新的东西,特此记录一下。

正确的操作如下所示:

  1. sed "/$1/c $2" a.log > b.log

这里边用到了位置参数变量,是想制作一个可以灵活控制替换内容的脚本,在外部执行脚本的时候首先输入要替换的被匹配的内容,其次是输入替换后的内容。
执行脚本的格式大概这样:

  1. bash aa.sh abc.url "abc.url=bcd"

在配置文件当中abc.url可能不是bcd,那么通过这个脚本,就能够把他改成bcd了。

这个地方有两个地方要注意的:

  • 1, sed后边是双引号,而不是单引号,如果单引号,则此命令会失败。原因是因为其中含有变量。
  • 2, 这个只是做一个预览式修改,并没有真正更改其中内容,所以需要导出,然后再倒回原文件。

weinxin

二丫讲梵 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于sed命令当中包含变量的坑

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK