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.comI 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/.*. test.txt 5 3 Answers
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/ file 1 Use sed -i 's/domain1/domain2/' infile.
If you want to replace *.domain1.com then change like:
sed 's/.*domain1/domain2/' infileMake sure at first attempt don't use -i which will replace in-place in your file.
Open file in vi editor. Then type below mentioned command on line mode of vi editor:
#vi file_name
example1.domain1.com
example2.domain1.com
example3.domain1.com
:%s/domain1/domain2/g hit enter nowDone!!
Note: domain1 is already existing string. domain2 is a new string.