0

I am writing a small programme whose functionality is like following. It uses a file named hist1.dat and the first step is like this

awk '{if(NR>1) s+=$2*($1-x); x=$1}END{print s}' hist1.dat >int1.txt

The hist1.dat is like following

0.259990113102 4
0.261752339307 10
0.263514565512 15
0.265276791717 35
0.267039017922 58
0.268801244127 84
0.270563470333 147
0.272325696537 217
0.274087922742 316
0.275850148947 410
0.277612375152 583
0.279374601357 750
0.281136827562 881
0.282899053767 1004
0.284661279972 1241

The int1.txt contains a value 79.9504 . The second step uses this value and process it further like following

awk '{if(NR>1) s+=$2*($1-x); print $1,s/c1; x=$1}' c1="$(cat int1.txt)" hist1.dat >cutoff1_org.txt

The cutoff1_org.txt looks like following

0.321668030277 0.832661
0.323430256483 0.851265
0.325192482688 0.867267
0.326954708893 0.882167
0.328716935097 0.895634
0.330479161302 0.906677
0.332241387507 0.917587
0.334003613712 0.927175
0.335765839917 0.935287
0.337528066122 0.9432
0.339290292327 0.94968
0.341052518532 0.955675
0.342814744737 0.961097
0.344576970942 0.96555
0.346339197147 0.969231

The next script uses the cutoff1_org.txt file and tries to find a value in column 1 if the corresponding value in column 2 is close to 0.95. This works well too

awk -v c=2 -v t=0.95 'NR==1{d=$c-t;d=d<0?-d:d;v=$c;next}{m=$c-t;m=m<0?-m:m}m<d{d=m;v=$1}END{print v}' cutoff1_org.txt >final_cutoff1.txt

The next two scripts will just use values from final_cutoff1.txt. Something like following

awk '{ if ($1 >= cutoff1) print $1 }' cutoff1="$(cat final_cutoff1.txt)" hist1.dat >hist_oc1.dat                                                                                                         
awk '{ if ($1 <= cutoff1) print $1 }' cutoff1="$(cat final_cutoff1.txt)" hist1.dat >hist_uc1.dat 

Now I want to put inside a loop like following

for i in {1..22}; do                                                                                                                                                                                                        
    awk '{if(NR>1) s+=$2*($1-x); x=$1}END{print s}' hist${i}.dat >int${i}.txt                                                                                                                                               
    awk '{if(NR>1) s+=$2*($1-x); print $1,s/c${i}; x=$1}' c${i}="$(cat int${i}.txt)" hist${i}.dat >cutoff${i}_org.txt                                                                                                       
    awk -v c=2 -v t=0.95 'NR==1{d=$c-t;d=d<0?-d:d;v=$c;next}{m=$c-t;m=m<0?-m:m}m<d{d=m;v=$1}END{print v}' cutoff${i}_org.txt >final_cutoff${i}.txt                                                                          
    awk '{ if ($1 >= cutoff${i}) print $1 }' cutoff${i}="$(cat final_cutoff${i}.txt)" hist${i}.dat >hist_oc${i}.dat                                                                                                         
    awk '{ if ($1 <= cutoff${i}) print $1 }' cutoff${i}="$(cat final_cutoff${i}.txt)" hist${i}.dat >hist_uc${i}.dat                                                                                                         
    cat hist_oc${i}.dat |stdev >stat_oc${i}.txt                                                                                                                                                                             
    cat hist_uc${i}.dat |stdev >stat_uc${i}.txt                                                                                                                                                                             
done

However I got an error like following

awk: cmd. line:1: {if(NR>1) s+=$2*($1-x); print $1,s/c${i}; x=$1}
awk: cmd. line:1:                                      ^ syntax error
awk: cmd. line:1: {if(NR>1) s+=$2*($1-x); print $1,s/c${i}; x=$1}
awk: cmd. line:1:                                               ^ syntax error
awk: cmd. line:1: { if ($1 >= cutoff${i}) print $1 }
awk: cmd. line:1:                    ^ syntax error

I think I have brain fade now. Seems like very easy error to fix. Can anyone please point out and help with it. Thanks a lot in advance

  • have a look here : https://stackoverflow.com/a/15787068/8344060 – kvantour Apr 27 '18 at 11:19
  • 3
    Possible duplicate of [Can we use shell variables in awk?](https://stackoverflow.com/questions/15786777/can-we-use-shell-variables-in-awk) – kvantour Apr 27 '18 at 11:20
  • awk '{if(NR>1) s+=$2*($1-x); print $1,s/c${i}; x=$1}' -v c${i}="$(cat int${i}.txt)" hist${i}.dat >cutoff${i}_org.txt something like that? – Soumendranath Bhakat Apr 27 '18 at 11:26
  • the problem is your `${i}` in your Awk. It is a shell variable and unknown to Awk. the link explains how to do it – kvantour Apr 27 '18 at 11:32
  • Sorry I didn't get it -v c${i}="$(cat int${i}.txt)" this is what I add in awk but still the problem exists – Soumendranath Bhakat Apr 27 '18 at 11:35
  • `awk '{if(NR>1) s+=$2*($1-x); print $1,s/c; x=$1}' c="$(cat int${i}.txt)" hist${i}.dat >cutoff${i}_org.txt` – kvantour Apr 27 '18 at 11:41
  • 2
    Clean up your question to get rid of all the irrelevant and duplicate information and just show us 1 awk script with 1 **small** input file and the assocatied expected output that demonstrate your problem, i.e. a [mcve]. – Ed Morton Apr 27 '18 at 11:53
  • Okay great thanks a lot I solved it. – Soumendranath Bhakat Apr 27 '18 at 12:22
  • You're really missing the point of using awk and have extremely inefficient and redundant code, even if so far it does produce the output you expect. As a very basic example, 2 passes of the input like `awk '{ if ($1 >= cutoff1) { print $1 }' in > out1` and `awk '{ if ($1 <= cutoff1) { print $1 }' in > out2` should be just 1 pass of the input: `awk '{ if ($1 == cutoff1) { print $1 > "out1"; print $1 > "out2" } else { print $1 > ($1 > cutoff1 ? "out1" : "out2"}' in`. Also see https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice – Ed Morton Apr 27 '18 at 12:55
  • Best I can tell without knowing more about your input, expected output, and requirements your shell loop containing 5 calls to awk should just be a single small awk script. Post a new question if you'd like help doing whatever you're doing the right way. – Ed Morton Apr 27 '18 at 12:57

0 Answers0