Search code examples
while-loopcshtcsh

tcsh error: while loop


This is a basic program but since I'm a newbie, I'm not able to figure out the solution.

I have a file named rama.xvg in the following format:

-75.635  105.879  ASN-2
-153.704  64.7089  ARG-3
-148.238  -47.6076  GLN-4
-63.2568  -8.05441  LEU-5
-97.8149  -7.34302  GLU-6
-119.276  8.99017  ARG-7
-144.198  -103.917  SER-8
-65.4354  -10.3962  GLY-9
-60.6926  12.424  ARG-10
-159.797  -0.551989  PHE-11
 65.9924  -48.8993  GLY-12
 179.677  -7.93138  GLY-13
..........
...........
-70.5046  38.0408  GLY-146
-155.876  153.746  TRP-147
-132.355  151.023  GLY-148
-66.2679  167.798  ASN-2
-151.342  -33.0647  ARG-3
-146.483  41.3483  GLN-4
..........
..........
-108.566  0.0212432  SER-139
 47.6854  33.6991  MET-140
 47.9466  40.1073  ASP-141
 46.4783  48.5301  SER-142
-139.17  172.486  LYS-143
  58.9514  32.0602  SER-144
  60.744  18.3059  SER-145
-94.0533  165.745  GLY-146
-161.809  177.435  TRP-147
 129.172  -101.736  GLY-148

I need to extract all the lines containing "ASN-2" in one file all_1.dat and so on for all the 147 residues.

If I run the following command in the terminal, it gives the desired output for ASN-2:

awk '{if( NR%147 == 1 ) printf $0 "\n"}' rama.xvg > all_1.dat

To avoid doing it repeatedly for all the residues, I have written the following code.

 #!/bin/tcsh
set i = 1
    while ( $i < 148)
    echo $i
    awk '{if( NR%147 == i ) printf $0 "\n"}' rama.xvg > all_"$i".dat
    @ i++

end

But this code prints the lines containing GLY-148 in all the output files.

Please let me know what is the error in this code. I think it is related to nesting.


Solution

  • In your awk-line the variable i is an awk-variable not shell variable! If you want use shell-variable $i you can do:

    awk -v i="$i" '{if( NR%147 == i ) printf $0 "\n"}' rama.xvg > all_"$i".dat
    

    But I think would better put your while-loop into awk:

    awk '{for (i=1; i<=147; i++) { if (NR%147==i) {printf $0 "\n" > ("all_" i ".dat") } } }' rama.xvg