statler
statler

Reputation: 1381

awk command within bash loop - recognizing double quote

I use a simple awk script to sum a value in a split of the fourth column. Works no problems.

awk '{split($4,NM,"-");if ($10>1) {TGC+=NM[2]}}END{print TGC}'  somefile.txt

When I try to loop it in BASH, it runs, but gives an answer of one.

for i in somedir; do echo $i; awk '{split($4,NM,"-");if ($10>1) {TGC+=NM[2]}}END{print TGC}'  $i  ;done

The problem is in the quotes within the split - how can I properly qualify it so awk recognizes the split?

Cheers statler

Upvotes: 0

Views: 762

Answers (1)

glenn jackman
glenn jackman

Reputation: 247062

I suspect you intended your for loop to examine the files in somedir, not the directory itself:

for i in somedir/*; do 
  echo "$i"
  awk '
    $10 > 1 {split($4, NM, "-"); TGC += NM[2]}
    END {print TGC}
  '  "$i"
done

Upvotes: 1

Related Questions