0

Below is the part of the code that is throwing error.

for (( p=0; p<${var2}; p++ ))
do

if [[ ${array2[$p]} == *"xyz"* ]]
then
    awk -F" " '{ array3[p]="123" };1' ${array1[$i]}
else
    awk -F" " '{ array3[p]="456" };1' ${array1[$i]}
fi

When i run the script in debug mode, i could see that the value of p is not being considered.

+ awk '-F ' '{ array3[p]="456" };1' oct.csv

I am running the script as ./script.sh and /bin/ksh is the shell used. Please help.

  • What is it you're trying to accomplish? – ilkkachu Oct 3 '16 at 19:55
  • 2
    I'm pretty confident you're after awk -v "p=$p". But btw, it is much better to post a working example or describe what you are after. We are just guessing what var2 or array2 may contain. – grochmal Oct 3 '16 at 20:08
  • I am trying to assign array3[p]="123" but since value of p is not getting retrieved, i want to know how to get it where p is a variable. – user2568967 Oct 3 '16 at 20:20
  • @user2568967 You're wanting to assign ${array3[$p]} value to "123" or "456" depending on if ${array2[$p]} is == "xyz", right? But why are you setting a variable inside the awk statement then not using it? Examples of what you want to accomplish with array3 would be helpful. Also as an FYI you don't need to use -F" "with awk as it uses space as the field separator by default. – Zachary Brady Oct 3 '16 at 20:53
  • I am trying to use the variable inside the awk array statement but not sure how to use it since i could not retrieve the value of p. Once it is retrieved, its place will be filled either with 123 or 456. – user2568967 Oct 3 '16 at 20:58
0

First of all, this is wrong:

awk -F" " '{ array3[p]="123" };1' ${array1[$i]}

${array1[$i]} is not a file, so you can't just throw it as first command line argument to awk. With variables you'd have to use either echo $VARIABLE or here string <<< like so

awk -F" " '{ array3[p]="123" };1' <<< ${array1[$i]}

Second, of all, there is a scope issue. array3[p]="123" only exists within awk code, so you can't assign anything to an outside of it. What you could do, however, simply do

array3[$p]="123"

It is also unclear where exactly $i comes from in your script. Overall, your question gives us a bit of code but doesn't provide any clues to fully answer your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.