仅具有特定路径的注释行

仅具有特定路径的注释行

我的文件包含如下行。

/opt/appdyn
/opt/appdyn/AppdExtension
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4/configscripts
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4/configscripts/upgradeExt.sh
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4/configscripts/IBMWebsphereMQExt.sh
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4/certs

我只想评论以下几行:/opt/appdyn/opt/appdyn/AppdExtension

预期的输出是

#/opt/appdyn
#/opt/appdyn/AppdExtension
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4/configscripts
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4/configscripts/upgradeExt.sh
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4/configscripts/IBMWebsphereMQExt.sh
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4/certs

答案1

要注释掉这些行,您可以使用sed.唯一的技巧是您还需要包含行尾字符$,否则会影响其他行:

$ cat file
/opt/appdyn
/opt/appdyn/AppdExtension
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4/configscripts
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4/configscripts/upgradeExt.sh
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4/configscripts/IBMWebsphereMQExt.sh
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4/certs
$ sed -e 's|/opt/appdyn$|#/opt/appdyn|' -i file
$ sed -e 's|/opt/appdyn/AppdExtension$|#/opt/appdyn/AppdExtension|' -i file
$ cat file
#/opt/appdyn
#/opt/appdyn/AppdExtension
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4/configscripts
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4/configscripts/upgradeExt.sh
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4/configscripts/IBMWebsphereMQExt.sh
/opt/appdyn/AppdExtension/wmqmonitor-7.0.4/certs

相关内容