sed 搜索并替换为 2 conditions/patterns
sed search and replace with 2 conditions/patterns
我有一个文件:
<a href="http://www.gnu.org/software/coreutils/">http://www.gnu.org/software/coreutils/</a>
<a href="../../3/glob.html" rel="nofollow">glob</a>
<a href="lxc-ls.html" rel="nofollow">lxc-ls</a>
我只需要替换下面:
<a href="lxc-ls.html" rel="nofollow">lxc-ls</a>
与
<a href="../lxc-ls.html" rel="nofollow">lxc-ls</a>
lxc-ls 可以是任何单词,因为我在需要替换的多个文件中有多个这样的链接。
我不想对其他 2 个链接进行任何更改。即
<a href="http://www.gnu.org/software/coreutils/">http://www.gnu.org/software/coreutils/</a>
<a href="../../3/glob.html" rel="nofollow">glob</a>
我尝试过的是:
$ sed '/html/ i\
..' file
但这附加到行首,另外排除2个URL的其他条件也没有填满。
这是来自其中一个文件的更真实的示例。
<b><a href="../../1/echoping.html" rel="nofollow">echoping</a></b>(1),
<b><a href="../../3/getaddrinfo.html" rel="nofollow">getaddrinfo</a></b>(3),
<b><a href="../../3/getaddrinfo_a.html" rel="nofollow">getaddrinfo_a</a></b>(3),
<b><a href="../../2/getpeername.html" rel="nofollow">getpeername</a></b>(2),
<b><a href="../../2/getsockname.html" rel="nofollow">getsockname</a></b>(2),
<b><a href="../../3/ping_setopt.html" rel="nofollow">ping_setopt</a></b>(3),
<b><a href="../../5/proc.html" rel="nofollow">proc</a></b>(5),
<b><a href="rds.html" rel="nofollow">rds</a></b>(7),
<b><a href="../../2/recv.html" rel="nofollow">recv</a></b>(2),
<b><a href="rtnetlink.html" rel="nofollow">rtnetlink</a></b>(7),
<b><a href="sctp.html" rel="nofollow">sctp</a></b>(7),
<b><a href="../../3/sctp_connectx.html" rel="nofollow">sctp_connectx</a></b>(3),
<b><a href="../../2/send.html" rel="nofollow">send</a></b>(2),
<b><a href="udplite.html" rel="nofollow">udplite</a></b>(7)
<a href="http://gnu.org/licenses/gpl.html">http://gnu.org/licenses/gpl.html</a>
<a href="http://translationproject.org/team/">http://translationproject.org/team/</a>
这里我只需要替换:
<b><a href="rds.html" rel="nofollow">rds</a></b>(7),
<b><a href="rtnetlink.html" rel="nofollow">rtnetlink</a></b>(7),
<b><a href="sctp.html" rel="nofollow">sctp</a></b>(7),
<b><a href="udplite.html" rel="nofollow">udplite</a></b>(7)
与:
<b><a href="../rds.html" rel="nofollow">rds</a></b>(7),
<b><a href="../rtnetlink.html" rel="nofollow">rtnetlink</a></b>(7),
<b><a href="../sctp.html" rel="nofollow">sctp</a></b>(7),
<b><a href="../udplite.html" rel="nofollow">udplite</a></b>(7)
使用sed
$ sed s'|"\([[:alpha:]].*\)|"../|' file
<b><a href="../../1/echoping.html" rel="../nofollow">echoping</a></b>(1),
<b><a href="../../3/getaddrinfo.html" rel="../nofollow">getaddrinfo</a></b>(3),
<b><a href="../../3/getaddrinfo_a.html" rel="../nofollow">getaddrinfo_a</a></b>(3),
<b><a href="../../2/getpeername.html" rel="../nofollow">getpeername</a></b>(2),
<b><a href="../../2/getsockname.html" rel="../nofollow">getsockname</a></b>(2),
<b><a href="../../3/ping_setopt.html" rel="../nofollow">ping_setopt</a></b>(3),
<b><a href="../../5/proc.html" rel="../nofollow">proc</a></b>(5),
<b><a href="../rds.html" rel="nofollow">rds</a></b>(7),
<b><a href="../../2/recv.html" rel="../nofollow">recv</a></b>(2),
<b><a href="../rtnetlink.html" rel="nofollow">rtnetlink</a></b>(7),
<b><a href="../sctp.html" rel="nofollow">sctp</a></b>(7),
<b><a href="../../3/sctp_connectx.html" rel="../nofollow">sctp_connectx</a></b>(3),
<b><a href="../../2/send.html" rel="../nofollow">send</a></b>(2),
<b><a href="../udplite.html" rel="nofollow">udplite</a></b>(7)
我有一个文件:
<a href="http://www.gnu.org/software/coreutils/">http://www.gnu.org/software/coreutils/</a>
<a href="../../3/glob.html" rel="nofollow">glob</a>
<a href="lxc-ls.html" rel="nofollow">lxc-ls</a>
我只需要替换下面:
<a href="lxc-ls.html" rel="nofollow">lxc-ls</a>
与
<a href="../lxc-ls.html" rel="nofollow">lxc-ls</a>
lxc-ls 可以是任何单词,因为我在需要替换的多个文件中有多个这样的链接。
我不想对其他 2 个链接进行任何更改。即
<a href="http://www.gnu.org/software/coreutils/">http://www.gnu.org/software/coreutils/</a>
<a href="../../3/glob.html" rel="nofollow">glob</a>
我尝试过的是:
$ sed '/html/ i\
..' file
但这附加到行首,另外排除2个URL的其他条件也没有填满。
这是来自其中一个文件的更真实的示例。
<b><a href="../../1/echoping.html" rel="nofollow">echoping</a></b>(1),
<b><a href="../../3/getaddrinfo.html" rel="nofollow">getaddrinfo</a></b>(3),
<b><a href="../../3/getaddrinfo_a.html" rel="nofollow">getaddrinfo_a</a></b>(3),
<b><a href="../../2/getpeername.html" rel="nofollow">getpeername</a></b>(2),
<b><a href="../../2/getsockname.html" rel="nofollow">getsockname</a></b>(2),
<b><a href="../../3/ping_setopt.html" rel="nofollow">ping_setopt</a></b>(3),
<b><a href="../../5/proc.html" rel="nofollow">proc</a></b>(5),
<b><a href="rds.html" rel="nofollow">rds</a></b>(7),
<b><a href="../../2/recv.html" rel="nofollow">recv</a></b>(2),
<b><a href="rtnetlink.html" rel="nofollow">rtnetlink</a></b>(7),
<b><a href="sctp.html" rel="nofollow">sctp</a></b>(7),
<b><a href="../../3/sctp_connectx.html" rel="nofollow">sctp_connectx</a></b>(3),
<b><a href="../../2/send.html" rel="nofollow">send</a></b>(2),
<b><a href="udplite.html" rel="nofollow">udplite</a></b>(7)
<a href="http://gnu.org/licenses/gpl.html">http://gnu.org/licenses/gpl.html</a>
<a href="http://translationproject.org/team/">http://translationproject.org/team/</a>
这里我只需要替换:
<b><a href="rds.html" rel="nofollow">rds</a></b>(7),
<b><a href="rtnetlink.html" rel="nofollow">rtnetlink</a></b>(7),
<b><a href="sctp.html" rel="nofollow">sctp</a></b>(7),
<b><a href="udplite.html" rel="nofollow">udplite</a></b>(7)
与:
<b><a href="../rds.html" rel="nofollow">rds</a></b>(7),
<b><a href="../rtnetlink.html" rel="nofollow">rtnetlink</a></b>(7),
<b><a href="../sctp.html" rel="nofollow">sctp</a></b>(7),
<b><a href="../udplite.html" rel="nofollow">udplite</a></b>(7)
使用sed
$ sed s'|"\([[:alpha:]].*\)|"../|' file
<b><a href="../../1/echoping.html" rel="../nofollow">echoping</a></b>(1),
<b><a href="../../3/getaddrinfo.html" rel="../nofollow">getaddrinfo</a></b>(3),
<b><a href="../../3/getaddrinfo_a.html" rel="../nofollow">getaddrinfo_a</a></b>(3),
<b><a href="../../2/getpeername.html" rel="../nofollow">getpeername</a></b>(2),
<b><a href="../../2/getsockname.html" rel="../nofollow">getsockname</a></b>(2),
<b><a href="../../3/ping_setopt.html" rel="../nofollow">ping_setopt</a></b>(3),
<b><a href="../../5/proc.html" rel="../nofollow">proc</a></b>(5),
<b><a href="../rds.html" rel="nofollow">rds</a></b>(7),
<b><a href="../../2/recv.html" rel="../nofollow">recv</a></b>(2),
<b><a href="../rtnetlink.html" rel="nofollow">rtnetlink</a></b>(7),
<b><a href="../sctp.html" rel="nofollow">sctp</a></b>(7),
<b><a href="../../3/sctp_connectx.html" rel="../nofollow">sctp_connectx</a></b>(3),
<b><a href="../../2/send.html" rel="../nofollow">send</a></b>(2),
<b><a href="../udplite.html" rel="nofollow">udplite</a></b>(7)