简体   繁体   中英

Replace text containing regular expression in terminal

I would like to replace in IP address with a sed command in terminal

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.

How could I check for strings containing the http:// and :8000 part with anything between, and replace it with the http://example.com ?

* is a quantifier that matches 0 or more occurrences of the pattern being quantified.

You want to match any amount of characters other than / and whitespace, that is, [^/[:space:]]* .

Use

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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