Q:

How do I replace string containing square brackets

Help me, please.
There is a file with such strings:
<b>[source:1:2:3]</b>
<b>[source:2:3:1]</b>
<b>[source:3:1:2]</b>
<b>[source:1:3:2]</b>

I only need to convert one line from this file. The result should be such a file:
<b>[source:1:2:3]</b>
<b>[source:2:3:1]</b>
<b>[result:3:1:2]</b>
<b>[source:1:3:2]</b>

Broke my head trying to escape [ with sed :(
Probably should be something like this:
cat test.txt |sed "s,<b>\[source:3:1:2\]</b>,<b>\[result:3:1:2\]</b>,g"

A:

EDIT: To look for specific string and then change line use following.
awk -v old_string="source" -v new_string="result" '/<b>\[source:3:1:2\]<\/b>/{sub(old_string,new_string)} 1' Input_file

OR to save results into same Input_file then try following.
awk -v old_string="source" -v new_string="result" '/<b>\[source:3:1:2\]<\/b>/{sub(old_string,new_string)} 1' Input_file > temp && mv temp Input_file

If you are ok with awk then try following.
awk 'FNR==3{sub(/source/,"result")} 1'  Input_file

OR if you want to have variables of current and new values in awk code then try following.
awk -v old_string="source" -v new_string="result" 'FNR==3{sub(old_string,new_string)} 1' Input_file

