【问题标题】:Search Replace Sed using wild card使用通配符搜索替换 Sed
【发布时间】:2020-06-24 14:27:45
【问题描述】:

您好,我是脚本新手,遇到以下问题

我需要更换20069.1216.0

HintPath..\packages\String.20069.1216.0\lib\net\Thoo.Tkc.dll/HintPath

这可以很好地用 $2 中提供的任何内容替换 20069.1216.0

`xargs sed -i 's/String.20.........0/String.'"${2}"'/g'`

我需要一种让 sed 搜索 **"String.*\lib\net\"** 的方法,其中 **String.****\lib...** 之间的任何内容都是通配符

这是我尝试过的

sed -i 's/String.*\/String.'"${2}"'/g' sed -i 's/String.*\\/String.'"${2}"'/g' sed -i 's/String.\(.*\)\\/String.'"${2}"'/g'

【问题讨论】:

  • 在除Thoo.Tkc.dll/HintPath 之外的任何地方都使用WinDoze \。这是故意的吗?
  • 我正在使用 bash 编辑用 .net 编写的程序的文件,该路径是类似的路径

标签: bash scripting git-bash


【解决方案1】:

我假设您在函数内部调用sed

所以试试这个代码:

#!/bin/bash

replace() {
  echo 'HintPath..\packages\String.20069.1216.0\lib\net\Thoo.Tkc.dll/HintPath' |\
  sed "s/String\.[0-9.]*/String\.${2}/"
}

replace dont_care filename

如果您的路径可能包含 --SNAPSHOT,此解决方案应该适合您:

#!/bin/bash

replace() {
  echo 'HintPath..\packages\String.20069.1216.0--SNAPSHOT\lib\net\Thoo.Tkc.dll/HintPath' |\
  sed "s/String[0-9.]*\(--SNAPSHOT\)\{0,1\}/String\.${2}/"
}

replace dont_care filename

【讨论】:

  • 你好,如果有时它看起来像这样 'String.20069.1216.0--SNAPSHOT' 我的搜索需要包含 --SNAPSHOT 并替换它
猜你喜欢
  • 2011-08-16
  • 1970-01-01
  • 2011-01-06
  • 2011-09-09
  • 1970-01-01
  • 2012-09-29
  • 2013-10-17
  • 1970-01-01
相关资源
最近更新 更多