2

I have trouble to replace the text inside a line using sed. here is the example.

<con:startTransactionRequestElement>2a07a832-5b4e-44d2-8826-64d8678f6226</con:startTransactionRequestElement>

i want to only change the content between <con:startTransactionRequestElement> and </con:startTransactionRequestElement> for example, change the above content to below:

<con:startTransactionRequestElement>abcdefegeawge</con:startTransactionRequestElement>

can someone tell me how to do that?

Thanks.

Feng
  • 41
  • 7
  • Are you asking how to modify a string based on a pattern match? – nzc Jun 02 '16 at 20:25
  • Here is something which may be helpful: http://unix.stackexchange.com/questions/112023/how-can-i-replace-a-string-in-a-files – nzc Jun 02 '16 at 20:28
  • i am sorry if my question is not clear. i just want to know how to replace the content inside a pattern such as replace the word between > and – Feng Jun 02 '16 at 20:28
  • Here is something which may be helpful: http://unix.stackexchange.com/questions/112023/how-can-i-replace-a-string-in-a-files – nzc Jun 02 '16 at 20:30

1 Answers1

3

With GNU sed:

sed 's|\(<con:startTransactionRequestElement>\).*\(</con:startTransactionRequestElement>\)|\1abcdefegeawge\2|' file

If you want to edit your file "in place" use sed's option -i.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • can i use variables inside\1 and \2 in your answer. for example if i want to use ${VAR} can it accept? i tried without luck. thanks. – Feng Jun 03 '16 at 01:58
  • Replace `abcdefegeawge` by `'"${VAR}"'`. See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus Jun 03 '16 at 04:38