awk error with indice when used in a loop
Question
I have the follwing situation.
awk '$0 != "Force for tool binder" # print all lines until string found
$0 =="Force for tool binder"{
print ; getline ; print; # go to 2 lines below the string
getline; getline < " forceState$j.k "; print}' dynaFile_Offen1.k > tempDynaFile.k # take the string from
#the file forceStates$j.k and replace in the main file, generating a temp file.
The problem is that here the j is a loop index, meaning that for the first case it is j=1. when I used it as forceStates1.k it works perfect but in the loop it is not taking the value.
I would be obliged for the suggestions.
La solution
$j
in '... " forceState$j.k " ...'
will not get expanded.
Is it a problem?
Autres conseils
The $j looks to be a shell script variable. As your awk script is within a single quoted section the shell will not substitute variables within the awk script. So awk is seeing a literal $j
. You need to change the shell quoting to permit substitution or probably more usefully pass it in on the command line eg:
awk -v loopctr=$j 'BEGIN {print loopctr}'
will print your loop counter each time as the value of j
was passed in the awk variable loopctr
here.