Friday, May 3, 2024
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 2250  / 2 Years ago, sun, september 25, 2022, 4:54:32

Replace all sub-domain names in a file with a different domain name using wildcard.



Ex : In a file, I have the following domains :



example1.domain1.com    
example2.domain1.com
example3.domain1.com


I want to replace these sub-domain names with domain2.com. I tried using the below sed command. It works fine if the file size is small. But for large file it looks like the command execution never ends. I want to use a wildcard here since the text to be replaced have the same domain name i.e domain1.com.



sed -i s/.*.domain1.com/domain2.com/g test.txt

More From » text-processing

 Answers
4

If you use .*.domain1.com as your pattern, you will essiantially match unwanted characters, because . means any character.

You want to replace only "word characters" (ASCII letters, digits or underscore) using w+.



As normal sed regex doesn't know about +, make sure to add -r for sed to use extended regex.



Also make sure you use quotation marks! Otherwise * might be interpreted by bash.

And you should escape . in the pattern, otherwise it will also match any character.



sed -i.bak -r 's/w+.domain1.com/domain2.com/g' file

[#8701] Monday, September 26, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pilun

Total Points: 270
Total Questions: 100
Total Answers: 94

Location: England
Member since Sat, Feb 13, 2021
3 Years ago
;