Skip to main content
added 188 characters in body
Source Link
user258795
user258795

I have an awk command which runs well on terminal: this awk command creates different file according to their column header. awk command:

for((i=2;i<5;i++)); do awk -v i=$i 'BEGIN{OFS=FS="\t"}NR==1{n=$i}{print $1,$i > n".txt"}' ${Batch}.result.txt done 

the same command when incorporated in a shell script shows error:

Syntax error: Bad for loop variable 

It worked the following way. I tried with she-bang as suggested but it repeated the error.

for i in 2 3 4; do awk -v i=$i 'BEGIN{OFS=FS="\t"}NR==1{n=$i}{print $1,$i n".txt"}' 

I have an awk command which runs well on terminal: this awk command creates different file according to their column header. awk command:

for((i=2;i<5;i++)); do awk -v i=$i 'BEGIN{OFS=FS="\t"}NR==1{n=$i}{print $1,$i > n".txt"}' ${Batch}.result.txt done 

the same command when incorporated in a shell script shows error:

Syntax error: Bad for loop variable 

I have an awk command which runs well on terminal: this awk command creates different file according to their column header. awk command:

for((i=2;i<5;i++)); do awk -v i=$i 'BEGIN{OFS=FS="\t"}NR==1{n=$i}{print $1,$i > n".txt"}' ${Batch}.result.txt done 

the same command when incorporated in a shell script shows error:

Syntax error: Bad for loop variable 

It worked the following way. I tried with she-bang as suggested but it repeated the error.

for i in 2 3 4; do awk -v i=$i 'BEGIN{OFS=FS="\t"}NR==1{n=$i}{print $1,$i n".txt"}' 
edited title
Link
200_success
  • 5.7k
  • 1
  • 28
  • 34

"Syntax error: Bad for loop variable" when trying to run awk command in a loop in a shell script

Source Link
user258795
user258795
Loading