1

I am trying to create a script in order to break down a file into 24. The "infoband.dat" contains the data of 24 bands that i want to plot, but rather than writing each band separately, it first writes all the 1st points of each band, then all the 2nd points, etc. My script was supposed to begin reading each line of the file, while count to 24 over and over until the end of the file. On the first iteration of the for loop, it would create a file with all the first lines out of those 24 line chunks, and it does it successfully. But the second iteration doesn't even start. What is breaking the loop?

  1 #!/bin/sh
  2 grep frequency band.yaml > infoband.dat
  3 contadora=0
  4 for i in {1..24} #loop to create the band file, 24 is the no. of bands
  5 do
  6    contadora=$((contadora+1))
  7    contadorb=0
  8    contadorc=0
  9    while read line
 10       do
 11          contadorb=$((contadorb+1))
 12          if [ $contadorb -eq 25 ]
 13          then
 14             contadorb=1
 15             contadorc=$((contadorc+1))
 16          fi
 19          if [ $contadora -eq $contadorb ]
 20          then
 21             echo $contadora $contadorb $contadorc "$line" >> band_$contadora.dat
 22          fi
 23       done < infoband.dat
 24   echo "file of the band " $contadora "is finished"
 25 done

Update: i got the code done using a different approach (the variable contadorc is useless btw):

  1 #!/bin/sh
  2 grep frequency band.yaml > infoband.dat
  3 nband=24
  4 contadorb=0
  5 contadorc=0
  6 while read line
  7 do
  8    contadorb=$((contadorb+1))
  9    if [ $contadorb -eq $((nband+1)) ]
 10    then
 11       contadorb=1
 12       contadorc=$((contadorc+1))
 13    fi
 14    echo " "$contadorb" "$line"    punto_q $contadorc">> test_infoband.dat
 15 done < infoband.dat
 16 for i in `seq 1 $nband`
 17 do
 18    echo $i $nband
 19    grep " $i " test_infoband.dat > banda_$i.dat
 20 done
2
  • I just thought of a workaround... although i'm interested in knowing why this loop is not working. Commented Jan 8, 2014 at 11:41
  • don't you need to reset contradora, otherwise line 19 will have values like 25, 26 , 27 .... after the first loop. Good luck. Commented Jan 8, 2014 at 11:52

2 Answers 2

1

/bin/sh doesn't do brace expansion, so your loop only has one iteration, in which i is set to the string {1..24}. Either change the hashtag to /bin/bash and/or run the script with bash, or use

for i in $(seq 1 24)

(assuming your system has the seq command, otherwise you may need to just hard-code the list, or use a while loop to explicitly increment and test the value of i).

Sign up to request clarification or add additional context in comments.

Comments

0

Did you try using the command "split"?

split -l 24 infoband.dat

2 Comments

I would, but the problem is that the infoband file has the data as follows: line i want for the first file line i want for the second file . . line i want for the 24th file line i want for the 1st file ...
Thanks, actually your comment made me think of a workaround: in my script i had been creating a test file that should reproduce each line of infoband, to see if it was reading correctly. After you said that i realized i can type the number of the band on each line, and then grep them separately now that the lines have that identifier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.