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

**URL:** <https://community.unix.com/t/sed-inside-the-awk-script-to-replace-a-string-in-the-array/376160>\
**Category:** UNIX for Beginners Q & A\
**Created:** [August 27, 2019, 3:33pm UTC](https://community.unix.com/t/sed-inside-the-awk-script-to-replace-a-string-in-the-array/376160 "2019-08-27T15:33:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhagya123](https://community.unix.com/letter_avatar/bhagya123/32/6_449c533f586939b6e5f246632a46421f.png) [@bhagya123](https://community.unix.com/u/bhagya123)\
**Post date:** [August 27, 2019, 3:33pm UTC](https://community.unix.com/t/sed-inside-the-awk-script-to-replace-a-string-in-the-array/376160/1 "2019-08-27T15:33:56Z")

</div>

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?

```nohighlight
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 :

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

```

Output:

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

```

---

<div class="post-metadata">

**Author:** ![rdrtx1](https://community.unix.com/user_avatar/community.unix.com/rdrtx1/32/2721_2.png) [@rdrtx1](https://community.unix.com/u/rdrtx1)\
**Post date:** [August 27, 2019, 4:48pm UTC](https://community.unix.com/t/sed-inside-the-awk-script-to-replace-a-string-in-the-array/376160/2 "2019-08-27T16:48:01Z")

</div>

try:

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

```

instead of:

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

```

---

<div class="post-metadata">

**Author:** ![bhagya123](https://community.unix.com/letter_avatar/bhagya123/32/6_449c533f586939b6e5f246632a46421f.png) [@bhagya123](https://community.unix.com/u/bhagya123)\
**Post date:** [August 27, 2019, 8:05pm UTC](https://community.unix.com/t/sed-inside-the-awk-script-to-replace-a-string-in-the-array/376160/3 "2019-08-27T20:05:16Z")

</div>

Tried this :

```nohighlight
               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 :

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

```

Tried below as well and got the same error

```nohighlight
  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++];
                   }
                }

```

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [August 27, 2019, 8:57pm UTC](https://community.unix.com/t/sed-inside-the-awk-script-to-replace-a-string-in-the-array/376160/4 "2019-08-27T20:57:21Z")

</div>

> [@bhagya123](#):
>
> Tried this :
> 
> ```plaintext
> 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 :
> 
> ```plaintext
> awk: syntax error near line 180
> awk: illegal statement near line 180
> 
> ```
> 
> Tried below as well and got the same error
> 
> ```plaintext
> 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).

---

<div class="post-metadata">

**Author:** ![bhagya123](https://community.unix.com/letter_avatar/bhagya123/32/6_449c533f586939b6e5f246632a46421f.png) [@bhagya123](https://community.unix.com/u/bhagya123)\
**Post date:** [August 27, 2019, 9:48pm UTC](https://community.unix.com/t/sed-inside-the-awk-script-to-replace-a-string-in-the-array/376160/5 "2019-08-27T21:48:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [August 27, 2019, 10:40pm UTC](https://community.unix.com/t/sed-inside-the-awk-script-to-replace-a-string-in-the-array/376160/6 "2019-08-27T22:40:24Z")

</div>

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:

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [August 27, 2019, 10:48pm UTC](https://community.unix.com/t/sed-inside-the-awk-script-to-replace-a-string-in-the-array/376160/7 "2019-08-27T22:48:18Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![bhagya123](https://community.unix.com/letter_avatar/bhagya123/32/6_449c533f586939b6e5f246632a46421f.png) [@bhagya123](https://community.unix.com/u/bhagya123)\
**Post date:** [August 28, 2019, 2:30pm UTC](https://community.unix.com/t/sed-inside-the-awk-script-to-replace-a-string-in-the-array/376160/8 "2019-08-28T14:30:04Z")

</div>

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

```nohighlight
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

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

 }

```

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [August 28, 2019, 4:11pm UTC](https://community.unix.com/t/sed-inside-the-awk-script-to-replace-a-string-in-the-array/376160/9 "2019-08-28T16:11:55Z")

</div>

Moderator comments were removed during original forum migration.
