开发者

awk command within bash loop - recognizing double quote

开发者 https://www.devze.com 2023-02-11 05:01 出处:网络
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

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?

Ch开发者_如何学JAVAeers statler


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
0

精彩评论

暂无评论...
验证码 换一张
取 消