简体   繁体   English

在终端中替换包含正则表达式的文本

[英]Replace text containing regular expression in terminal

I would like to replace in IP address with a sed command in terminal我想用终端中的 sed 命令替换 IP 地址

sed -i "2 s=http://*:8000=http://example.com=g" file.txt;

Where the * can change. * 可以改变的地方。 I thought I could use * to replace any characters between the http:// and :8000, but it behaves like plain text.我以为我可以使用 * 来替换 http:// 和 :8000 之间的任何字符,但它的行为类似于纯文本。

How could I check for strings containing the http:// and :8000 part with anything between, and replace it with the http://example.com ?我如何检查包含 http:// 和 :8000 部分的字符串,并将其替换为http://example.com

* is a quantifier that matches 0 or more occurrences of the pattern being quantified. *是一个量词,匹配 0 次或多次出现的被量化的模式。

You want to match any amount of characters other than / and whitespace, that is, [^/[:space:]]* .您想要匹配除/和空格以外的任意数量的字符,即[^/[:space:]]*

Use使用

sed -i "2 s=http://[^/[:space:]]*:8000=http://example.com=g" file.txt;
                   <------------>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM