sed inside the awk script to replace a string in the array

The requirement is i need to find an array value matching with pattern {5:{ , replace that with 5: and reassign that to same array index and print it.

I write something like below and the issue is sed command is not working. If i replace " with "`" the script gives syntax error.how can i achieve this?

for (i=msgtkn_20_lnno; i<msg_lnno;)
{
if(msg_arr ~ /^{5:{/ )
{
tag5_before_value= msg_arr;
msg_arr="echo $tag5_before_value | sed 's/{5:{/5:/g'";
printf "after conversion is %s/n", abc > "LOG.txt";
print msg_arr[i++];
}
else
{
print msg_arr[i++];
}

}

sample :

{1:36rirfmvhfgf
:20:687nkj
:56:6979840
{5:{CBS:.....}}

Output:

{1:36rirfmvhfgf
:20:687nkj
:56:6979840
5:CBS:.....}}

try:

gsub("{5:{", "5:", msg_arr);

instead of:

tag5_before_value= msg_arr;
msg_arr="echo $tag5_before_value | sed 's/{5:{/5:/g'";

Tried this :

               for (i=msgtkn_20_lnno; i<msg_lnno;)
                {
                   if( msg_arr ~ /^{5:{/ )
                   {
                   msg_arr= gsub("{5:{", "5:", msg_arr);
                   print msg_arr[i++];
                   }
                   else
                   {
                   print msg_arr[i++];
                   }
                }

Error :

awk: syntax error near line 180
awk: illegal statement near line 180

Tried below as well and got the same error

  for (i=msgtkn_20_lnno; i<msg_lnno;)
                {
                   if( msg_arr ~ /^{5:{/ )
                   {
                  gsub("{5:{", "5:", msg_arr); print msg_arr[i++];
                   }
                   else
                   {
                   print msg_arr[i++];
                   }
                }

With what you have shown us, we have to do a lot of guessing to try to figure out what might be going wrong. Please, whenever you start a thread, tell us what operating system and shell you're using. And, when showing us a fragment of an awk script with associated diagnostic messages, indicate which line of the code you have shown us is the one that the diagnostic refers to (in this case, which line is line #180).

Sorry..The error is right at the gsub command in both cases.

below is my unix os version

SunOS unixs1095 5.10 Generic_150400-62 sun4v sparc sun4v

OK.

First, use /usr/xpg4/bin/awk or nawk ; not awk when you're using any Solaris/SunOS system. (I strongly recommend /usr/xpg4/bin/awk since it meets all of the POSIX standard's requirements.)

Second, while sed uses Basic Regular Expressions (BREs), awk uses Extended Regular Expressions (EREs). And, in EREs, the opening brace character ( { ) is special and needs to be escaped when you are looking for a literal opening brace character. The code rdrtx1 suggested may work on some systems, but is not portable and (according to the standards) produces undefined behavior.

Third, since you are looking for a single occurrence of the string {5:{ , there is no need to use gsub() (which looks for and attempts to find and replace as many occurrences of a matching pattern as it can find).

Try this instead:

for (i=msgtkn_20_lnno; i<msg_lnno; i++) {
	if( msg_arr ~ /^[{]5:[{]/ ) {
		sub("[{]5:[{]", "5:", msg_arr);
	}
	print msg_arr;
}

Note that I also moved the i++ into the for statement instead of hiding the loop increment in your print statements and I moved the print statement out of both branches of the if statement so it is more obvious that the same print is needed whether or not a match is found.

P.S. You might also note that the following code will produce the same results as the code shown in post #6:

for (i=msgtkn_20_lnno; i<msg_lnno; i++) {
	sub("^[{]5:[{]", "5:", msg_arr);
	print msg_arr;
}

I tried the script with /usr/xpg4/bin/awk , But it is throwing error at a diff place.

Error :
/usr/xpg4/bin/awk: line 2 (): syntax error "unk" in //

Below is the first 10 lines from the code and its throwing error at line 2, Do you know what is wrong here?
The same code worked fine for just awk

BEGIN {
 FS="|$@#!|$@#!";
 line_num=0;
 msg_start=0;
 msg_end=0;

 }
Moderator comments were removed during original forum migration.