-1

I have input like below

<tag col1 = "" col2 = "" >
<Data>
</tag end>
<tag1 col1 = "" col2 = "" col3 = "" >  Here col1 can have some data(like col1 = "Data can be available") or it cane be empty (like col1 = "")
<data>
</tag1>
<tag2 col1 = "" col2 = "" >
<Data>
</tag2>

OutPut like below

<tag>
<Data>
</tag>
<tag1 col1 = "Updated Comments" col2 = "" col3 = "" >
<data>
</tag1>
<tag2 col1 = "" col2 = "" >
<Data>
</tag2>

I need to update just col1 on tag1 with updated comments and input can be empty or can have some other characters for col1 but I need to replace that col1 alone with updated comments

I tried below but it is now working as expected. If I am trying below one the other values (col2 = "" etc) in the line is getting truncated

sed -e 's/tag1 col1 =".*"/col1 =\"Updated Comments\"/' test_file.txt

1 Answers1

0

The output is little confusing as it makes it look like all the other xml tags need to be modified.

But your question/ sed command talks only about replacing the tag1 contents. the .* in your sed pattern match is greedy and will try to match as much as possible. You need to pattern match everything except the ".

> echo '<tag1 = "current comment" col1 = "">' | sed -e 's/tag1[ ]*=[ ]*"[^"]*"/tag1=\"updated description\"/g'
<tag1="updated description" col1 = "">

> echo '<tag1 = "" col1 = "">' | sed -e 's/tag1[ ]*=[ ]*"[^"]*"/tag1=\"updated description\"/g'
<tag1="updated description" col1 = "">
Ashok
  • 66
  • 5
  • I just updated with correct columns in tag1. Previously I missed col1. Please check and let me know which one will work now. Thank you – Satish Varadha Jul 01 '22 at 12:22
  • Hi @SatishVaradha, you can use `sed -e 's/tag1[ ]*=[ ]*"[^"]*"/tag1=\"updated description\"/g'` and let me know if it works. – Ashok Jul 08 '22 at 11:00