I have a file input.txt with the following sample data. I am trying to pattern match and assign the value to a variable. The problem is that I the assigned values are not retained outside of the nawk loop. Is there away for me to retain the variable values within the nawk loop to process later in the script (outside the nawk loop) ?
Thanks in advance for your help.
/tmp > cat input.txt
VAR1 Hello
VAR2 World
/tmp >
/tmp > cat match.ksh
cat input.txt | nawk '{
if (index($0, "VAR1") > 0) my_var1= $2
if (index($0, "VAR2") > 0) my_var2= $2
} END {
printf ("%5s %5s\n",
my_var1,
my_var2) ;
}' >> exec_out.log
v_var1=$my_var1
v_var2=$my_var2
echo v_var1=$v_var1 v_var2=$v_var2
/tmp >
/tmp >
/tmp > match.ksh
v_var1= v_var2= [ Ideally I want v_var1=Hello v_var2=World in the output if values are retained ]
/tmp >
/tmp >
/tmp > cat exec_out.log
Hello World
/tmp >
/tmp >
Thanks in advance for your help.
/tmp > cat input.txt
VAR1 Hello
VAR2 World
/tmp >
/tmp > cat match.ksh
cat input.txt | nawk '{
if (index($0, "VAR1") > 0) my_var1= $2
if (index($0, "VAR2") > 0) my_var2= $2
} END {
printf ("%5s %5s\n",
my_var1,
my_var2) ;
}' >> exec_out.log
v_var1=$my_var1
v_var2=$my_var2
echo v_var1=$v_var1 v_var2=$v_var2
/tmp >
/tmp >
/tmp > match.ksh
v_var1= v_var2= [ Ideally I want v_var1=Hello v_var2=World in the output if values are retained ]
/tmp >
/tmp >
/tmp > cat exec_out.log
Hello World
/tmp >
/tmp >