Skip to main content
added 27 characters in body
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60

Using GNU sed for -z, -E, \s shorthand for space, and word boundaries \< and \>:

$ sed -Ez 's/\<(index=)restricted_security(\s+sourcetype=forcepoint)\>/\1newname\2/g' file > o1

$ diff file o1
28c28
<           <query>index=restricted_security
---
>           <query>index=newname
81c81
<           <query>index=restricted_security
---
>           <query>index=newname

or if you wanted the 2 strings concatenated onto a single line (it's not clear from your question):

$ sed -Ez 's/\<(index=)restricted_security\s+(sourcetype=forcepoint)\>/\1newname \2/g' file > o1

$ diff file o1
28,29c28
<           <query>index=restricted_security
< sourcetype=forcepoint
---
>           <query>index=newname sourcetype=forcepoint
81,82c80
<           <query>index=restricted_security
< sourcetype=forcepoint
---
>           <query>index=newname sourcetype=forcepoint

Using GNU sed for -z, -E and word boundaries \< and \>:

$ sed -Ez 's/\<(index=)restricted_security(\s+sourcetype=forcepoint)\>/\1newname\2/g' file > o1

$ diff file o1
28c28
<           <query>index=restricted_security
---
>           <query>index=newname
81c81
<           <query>index=restricted_security
---
>           <query>index=newname

or if you wanted the 2 strings concatenated onto a single line (it's not clear from your question):

$ sed -Ez 's/\<(index=)restricted_security\s+(sourcetype=forcepoint)\>/\1newname \2/g' file > o1

$ diff file o1
28,29c28
<           <query>index=restricted_security
< sourcetype=forcepoint
---
>           <query>index=newname sourcetype=forcepoint
81,82c80
<           <query>index=restricted_security
< sourcetype=forcepoint
---
>           <query>index=newname sourcetype=forcepoint

Using GNU sed for -z, -E, \s shorthand for space, and word boundaries \< and \>:

$ sed -Ez 's/\<(index=)restricted_security(\s+sourcetype=forcepoint)\>/\1newname\2/g' file > o1

$ diff file o1
28c28
<           <query>index=restricted_security
---
>           <query>index=newname
81c81
<           <query>index=restricted_security
---
>           <query>index=newname

or if you wanted the 2 strings concatenated onto a single line (it's not clear from your question):

$ sed -Ez 's/\<(index=)restricted_security\s+(sourcetype=forcepoint)\>/\1newname \2/g' file > o1

$ diff file o1
28,29c28
<           <query>index=restricted_security
< sourcetype=forcepoint
---
>           <query>index=newname sourcetype=forcepoint
81,82c80
<           <query>index=restricted_security
< sourcetype=forcepoint
---
>           <query>index=newname sourcetype=forcepoint
Source Link
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60

Using GNU sed for -z, -E and word boundaries \< and \>:

$ sed -Ez 's/\<(index=)restricted_security(\s+sourcetype=forcepoint)\>/\1newname\2/g' file > o1

$ diff file o1
28c28
<           <query>index=restricted_security
---
>           <query>index=newname
81c81
<           <query>index=restricted_security
---
>           <query>index=newname

or if you wanted the 2 strings concatenated onto a single line (it's not clear from your question):

$ sed -Ez 's/\<(index=)restricted_security\s+(sourcetype=forcepoint)\>/\1newname \2/g' file > o1

$ diff file o1
28,29c28
<           <query>index=restricted_security
< sourcetype=forcepoint
---
>           <query>index=newname sourcetype=forcepoint
81,82c80
<           <query>index=restricted_security
< sourcetype=forcepoint
---
>           <query>index=newname sourcetype=forcepoint