用sed用另一条路径替换路径

时间:2011-12-02 21:16:10

标签: replace sed path

我有一个包含以下行的文件:

some /foo/foo/foo some
some=/foo/foo/foo some
some /foo/foo/foo/ some
some=/foo/foo/foo/ some
some "/foo/foo/foo" some
some="/foo/foo/foo" some
some "/foo/foo/foo/" some
some="/foo/foo/foo/" some
some '/foo/foo/foo' some
some='/foo/foo/foo' some
some '/foo/foo/foo/' some
some='/foo/foo/foo/ some
some (/foo/foo/foo) some
some=(/foo/foo/foo) some

如何找到并将此路径替换为带有'frame'的路径,如:

some [p]/foo/foo/foo[;p] some
some=[p]/foo/foo/foo[;p] some
some [p]/foo/foo/foo/[;p] some
some=[p]/foo/foo/foo/[;p] some
some "[p]/foo/foo/foo[;p]" some
some="[p]/foo/foo/foo[;p]" some
some "[p]/foo/foo/foo/[;p]" some
some="[p]/foo/foo/foo/[;p]" some
some '[p]/foo/foo/foo[;p]' some
some='[p]/foo/foo/foo[;p]' some
some '[p]/foo/foo/foo/[;p]' some
some='[p]/foo/foo/foo/[;p] some
some ([p]/foo/foo/foo[;p]) some
some=([p]/foo/foo/foo[;p]) some

我写了几乎所有的例子,如果有人可能想设置子问题。 注意:路径完全是任意的。我不知道路径是什么。 我应该只知道它们应该被替换为带有'frame'的路径

3 个答案:

答案 0 :(得分:2)

这也可能有效:

 sed 's,/.*/[^")'\'' ]*,[p]&[;p],' file

说明:

使用,作为s命令分隔符。使用第一个/锚定正则表达式。然后使用贪婪的*吞下最后一个/的所有内容。再次使用*的贪婪来抓取非分隔符。然后在匹配的正则表达式的任一侧放置文本。这也强调了在引用命令中使用“打孔”以包含单引号'\''

答案 1 :(得分:1)

你提到过:

  

注意:路径完全是任意的。我不知道路径是什么。

我假设路径应采用\w格式(a-z A-Z 0-9或_)。然后这个sed行:

 sed -r 's#(/[a-zA-Z0-9_/]+)#[p]\1[;p]#g' yourFile

将完成这项工作。

使用您的示例查看测试。 (注意我对输入进行了一些更改,使其“任意”)

kent$  cat t
some /foo/foo/fo3o some
some=/foo/foo/fdoo some
some /foo/foo/fxoo/ some
some=/foo/fofo/foo/ some
some "/foxo/foo/foo" some
some="/foo/ffoo/foo" some
some "/foo/foxo/foo/" some
some="/foo/f6oo/foo/" some
some '/foo/fo7o/foo' some
some='/foo/foo7/foo' some
some '/foxo/foo/foo/' some
some='/fo0o/f_oo/foo/ some
some (/foo/f99oox/foo) some
some=(/fo234o/fdoo/fd_oo) some'

kent$  sed -r 's#(/[a-zA-Z0-9_/]+)#[p]\1[;p]#g' t
some [p]/foo/foo/fo3o[;p] some
some=[p]/foo/foo/fdoo[;p] some
some [p]/foo/foo/fxoo/[;p] some
some=[p]/foo/fofo/foo/[;p] some
some "[p]/foxo/foo/foo[;p]" some
some="[p]/foo/ffoo/foo[;p]" some
some "[p]/foo/foxo/foo/[;p]" some
some="[p]/foo/f6oo/foo/[;p]" some
some '[p]/foo/fo7o/foo[;p]' some
some='[p]/foo/foo7/foo[;p]' some
some '[p]/foxo/foo/foo/[;p]' some
some='[p]/fo0o/f_oo/foo/[;p] some
some ([p]/foo/f99oox/foo[;p]) some
some=([p]/fo234o/fdoo/fd_oo[;p]) some'

答案 2 :(得分:0)

这可能会提供一些帮助:

[jaypal:~/Temp/tem] cat file
some /foo/foo/foo some
some=/foo/foo/foo some
some /foo/foo/foo/ some
some=/foo/foo/foo/ some
some "/foo/foo/foo" some
some="/foo/foo/foo" some
some "/foo/foo/foo/" some
some="/foo/foo/foo/" some
some '/foo/foo/foo' some
some='/foo/foo/foo' some
some '/foo/foo/foo/' some
some='/foo/foo/foo/ some
some (/foo/foo/foo) some
some=(/foo/foo/foo) some

[jaypal:~/Temp/tem] sed 's@/foo/foo/foo/\?@[p]&[;p]@' file
some [p]/foo/foo/foo[;p] some
some=[p]/foo/foo/foo[;p] some
some [p]/foo/foo/foo/[;p] some
some=[p]/foo/foo/foo/[;p] some
some "[p]/foo/foo/foo[;p]" some
some="[p]/foo/foo/foo[;p]" some
some "[p]/foo/foo/foo/[;p]" some
some="[p]/foo/foo/foo/[;p]" some
some '[p]/foo/foo/foo[;p]' some
some='[p]/foo/foo/foo[;p]' some
some '[p]/foo/foo/foo/[;p]' some
some='[p]/foo/foo/foo/[;p] some
some ([p]/foo/foo/foo[;p]) some
some=([p]/foo/foo/foo[;p]) some
[jaypal:~/Temp/tem]