0

I currently have a for loop in bash as such:

for i in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 
   do 
   touch $i.txt
   awk '{if ($2 == "$i") print $1;}' results.txt > $i.txt
done

What I want it to do is take the time (stored in column 2 of results.txt), create a file and then print out column one (from results) into the new file. When I do this manually (just for one instance, say i = 01) it works fine, but as soon as I try to loop it, all the files are empty? Can someone help me understand what's happening?

Thanks!

Brandon Barry
  • 61
  • 1
  • 6
  • 1
    Also tangentially https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable – tripleee Jun 04 '20 at 06:40
  • There is also the use of octal numbers `01 02 03 04 05 06 07 08 09 ` which may give surprising results... – David C. Rankin Jun 04 '20 at 06:47
  • So I've read the answer to that question, oguz and am not seeing the results that I'd like to see still. I've updated my code to be awk -v i=$i '...' and it's still not working. – Brandon Barry Jun 04 '20 at 06:48
  • The octal numbers are neccesary as the time is given in 24 hours. I don't understand how it will give surprising results? – Brandon Barry Jun 04 '20 at 06:48
  • If you were not expecting them -- as you are they are fine. The use of the shell variable should be quoted in `awk`, e.g. `awk -v n="$i" '{if ($2 == n) print $1;}'` – David C. Rankin Jun 04 '20 at 06:52
  • Ok thanks David. I've updated my code to your suggestions and it works! Thank you – Brandon Barry Jun 04 '20 at 06:58

0 Answers0