How to assign a value to a variable in awk scripting?

Hi,

I am trying to assign a value using below command and it is assigning the command to the variable not the output of the command?

out_value="echo $0 | cut -c 9-11";

How can i assign the output to the variable instead of whole command?

This is inside my awk script

How about:

out_value=$( echo $0 | cut -c 9-11 )

Its giving the syntax error.

--- Post updated at 06:27 PM ---

This is how my awk script looks like :

AWK script:

BEGIN {
 FS="|$@#!|$@#!";
 lno=0;
 m_start=0;
m_end=0;
 }
{
lno=lno+1;
  if ( $0 ~ /^ABC OUT:/ )
  {
    out_value=$( echo $0 | cut -c 9-11 )
    if (m_start == 1 && m_end == 0)
     {
        msg_arr[msg_lnno++]=$0;
     }
   }
out_value=substr($0, 9, 3);

There's no point using cut inside awk.

out_value=substr($1,9,3)

...but for the record you could use backticks, variable=`command`; ...at a very large performance cost.

1 Like

Thanks..Actually i tried with substr and it was working..

Later i encountered an error and thought that is related to to the substr function. But looks like it caused by something else.

Thanks for the response !!

--- Post updated at 07:33 PM ---

Similar to that I am also trying an awk array and wanted to check if the array value match string "{5:{" , replace with 5: and assign it back to same array and print it.

But below code is not working its not able to replace the array value , instead its assigning whole command to msg_arr and printing it

any solution for 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++];
                }

              }

as soon as i use "`" its giving me syntax error

repeat from another post.

try:

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

instead of:

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