regex - sed 在特定匹配行的末尾附加一些内容

标签 regex linux sed

我有包含以下文本的文件。

200.200.200.253
80/tcp:filtered:http
443/tcp:filtered:https

200.200.200.254
80/tcp:filtered:http
443/tcp:filtered:https

web.example.com (200.200.200.250)
80/tcp:filtered:http
443/tcp:filtered:https

我想使用 sed 在 IP 地址末尾添加 (:)。所以它看起来像下面这样。

 200.200.200.253 :
 80/tcp:filtered:http
 443/tcp:filtered:https

 200.200.200.254 :
 80/tcp:filtered:http
 443/tcp:filtered:https

 web.example.com (200.200.200.250) :
 80/tcp:filtered:http
 443/tcp:filtered:https

我对 web.example.com (200.200.200.250) 正则表达式匹配有疑问。

我希望这一行看起来像 web.example.com (200.200.200.250) :


我得到了答案,这是我尝试过的。

 sed -e 's/[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}/&:/' file.txt

最佳答案

我建议你使用

<罢工>

<罢工>
sed -r 's/^([0-9]+\.){3}[0-9]+$/& :/g' filename

<罢工>

sed -r 's/([0-9]+\.){3}[0-9]+.*/& :/g' filename

关于regex - sed 在特定匹配行的末尾附加一些内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13038079/

相关文章:

linux - Selenium RC,在 Linux 上,有没有办法让所有浏览器实例在后台运行?

linux - fedora 中没有 authorized_keys 文件

regex - sed(或awk)正则表达式来替换匹配的子字符串

c++ - 正则表达式NFA,用于未定义的 token 顺序

javascript - 使用正则表达式,如何检查倒数第二个字符是否为奇数

linux - Fail2Ban 在 10 分钟后解禁。 bantime 没有警告?

regex - 使用 sed 匹配正则表达式

regex - 我可以在 SED 地址中乘以模式匹配来打印吗

python - 基于条件的子字符串替换-python

java - 将java源文件合并为一个