0

I have the following array

components=('xx' 'xy' 'xz' 'yx''yy''yz''zx''zy''zz')

And I'm doing the following loop for all components

for i in "${components[@]}"
    do
    if [ ${i: -1} == "x" ]; then
    awk '$1 == "*" && $2 == ${i: -1} {v=$3} END {print v}' ${i}_E_cutoff_$((100*$a))_eV/$systemID.castep >> Stress_${i}_vs_Ecutoff_convergence.txt
    elif [ ${i: -1} == "y" ]; then
    awk '$1 == "*" && $2 == ${i: -1} {v=$4} END {print v}' ${i}_E_cutoff_$((100*$a))_eV/$systemID.castep >> Stress_${i}_vs_Ecutoff_convergence.txt
    elif [ ${i: -1} == "z" ]; then
    awk '$1 == "*" && $2 == ${i: -1} {v=$5} END {print v}' ${i}_E_cutoff_$((100*$a))_eV/$systemID.castep >> Stress_${i}_vs_Ecutoff_convergence.txt
    fi      
    done

How come awk tells me I have a syntax error for

$2 == ${i: -1}
3
  • I think you want spaces between the "elements" yy''yz''zx''zy''zz'. Commented Jul 13, 2019 at 12:10
  • I tried to modify your logic (have one awk command without if-elif-elif-fi), but I don't see what you really want. I think $((100*$a)) is not relevant for your question. You use $1 == "*", is this the first field of a line from your inputfile or do you call your loop with variabeles? It seems that you want this for component xy: Look for the last line in the xy_ inputfile that starts with * xy (* not a wildcard) and append the 4th field of that line to your output. Is this what you want? Commented Jul 13, 2019 at 12:41
  • Adding a sample input and desired output will make your question easier to understand. And while you are editing your question, you might want to change the input and output files to ${i}.in and ${i}.out. Commented Jul 13, 2019 at 12:43

1 Answer 1

1

The reason you are getting that syntax error is because you are unable to use outside variables inside of an awk command just by themselves. You first need to tell awk about them.

Here is a really good post about how do to this: How do I use shell variables in an awk script?

Using the above answer you could do something like the following:

awk -v var=${i: -1} '$1 == "*" && $2 == var {v=$3} {print v}' ${i}_E_cutoff_$((100*$a)) _eV/$systemID.castep >> Stress_${i}_vs_Ecutoff_convergence.txt

where -v var=${i: -1} is you registering the variable for awk to use.

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.