Home > Software design >  How to use sed/grep to get all letter to the left of a certain word until it hits a space?
How to use sed/grep to get all letter to the left of a certain word until it hits a space?

Time:01-03

I got a text file that includes words like

interesting.website.com
cool.website.org
nice.website.de

and I would like to change them to

new.website.com
new.website.org
new.website.de

(These are surrounded by spaces). I want to use sed and/or grep.

I want to select all characters to the left of website. Is there a way to select all the characters left of it until it hits a space?

I already tried to select all characters inbetween a space and website, but since there can be several spaces before the word these will not give consistent results, e.g. with lines like this

This is my website: cool.website.org

CodePudding user response:

Like this?

$ cat merlon 
This is interesting.website.com yadda yadd ... 
And   cool.website.org que?
Oh yeah - more nice.website.de haha
$ grep -oP '[^ ] \.website\.[^ ] ' merlon
interesting.website.com
cool.website.org
nice.website.de

CodePudding user response:

You should use sed if you want to change the content

sed -E 's/[^ ] (\.website)/new\1/' file

result

$ echo 'This is my website: cool.website.org' | sed -E 's/[^ ] (\.website)/new\1/'
This is my website: new.website.org
  • Related