awk gsub simple problem

Hi

New to shell script and awk and need assistance on this problem. I need to use a variable to substitute a string in an external file and write the changed info to another file.

At first I did not know if you could use a variable as the sub value but the following showed me that I can.

echo "HI FROM STEVE" | awk '{ print $2; gsub(/STEVE/, $2); print }'

[LEFT]FROM
HI FROM FROM

This is the code that I have

ls -ltd @EOD4401* | sort +9 | head -n1 |
awk ' {{ {print $9 } print } ; gsub(/jobname/,$9 ); print }'
BPO_File_Template > cvtestfile

I have multiple directories as @EOD4401_0001, @EOD4401_0002 etc etc and I need to get the highest one and simply replace all text "jobname" in the file BPO_FILE_Template and write out to cvtestfile

The template file has

/mydir1/jobname
/midir2/jobname

but what gets written to cvtestfile is

/midir1/
/mydir2/

What am I doing wrong?

Thanks in advance

[/LEFT]

You are feeding awk from two sides: stdin and the file input from BPO_File_Template. Apparently in that case stdin gets ignored:

$> awk '{print}' <(echo 1 2 3 4)
1 2 3 4

$> echo "a b c d"|awk '{print}'
a b c d

$> echo "a b c d"|awk '{print}' <(echo 1 2 3 4)
1 2 3 4

Thanks for the reply. At least it stops me banging my head against the wall. Have removed awk and simply "cut" the column I need

CMD1="ls -ltd @EOD4401* | sort +9 | head -n1 "
LINE1=`eval $CMD1`

jobname_rep=`echo $LINE1 | cut -d ' ' -f9`

sed -e "s/jobname/$jobname_rep/g" -e "s/0000/4401/g" BPO_File_Template > cvtestfile

Am sure I can make it simpler but this works for me..

Still would be interested if awk could do it

Something like this?

awk ' BEGIN {"ls -ltd @EOD4401* | sort +9 | head -n1" | getline cmd; split(cmd, a)} {
  gsub("jobname", a[9]); gsub("0000", "4401")
}1' BPO_File_Template > cvtestfile