For loop in awk, a bug or something else?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loop in awk, a bug or something else?
# 1  
Old 07-21-2010
For loop in awk, a bug or something else?

Seems for loop in awk has bug?

For first awk, i expect to get 4 output: 1.5, 1.4, 1.3, 1.2, but there are only 3.

Code:
$ awk 'BEGIN {for (i=1.5;i>=1.2;i-=0.1) print i}'
1.5
1.4
1.3

$ awk 'BEGIN {for (i=15;i>=12;i-=1) print i}'
15
14
13
12

# 2  
Old 07-21-2010
Quote:
Originally Posted by rdcwayx
Seems for loop in awk has bug?

For first awk, i expect to get 4 output: 1.5, 1.4, 1.3, 1.2, but there are only 3.

Code:
$ awk 'BEGIN {for (i=1.5;i>=1.2;i-=0.1) print i}'
1.5
1.4
1.3

It is not a bug, it has to do with floating points. The are not as precise as you might think, due to the way they are represented.
That's why you shouldn't use them as a control of a loop.
This User Gave Thanks to Aia For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk within a for loop

Hello, I currently have managed to get an awk function working inside a for loop that allows me to combine two files based on their headings but what I have not been able to do is print the output to files with variable names. awk ' NR==FNR {a=$0; next} /^>/ {$0 = $0" "a;} ... (2 Replies)
Discussion started by: Allie_gastrator
2 Replies

2. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

3. Shell Programming and Scripting

awk for loop

Hi All, i have three files inputfilehello1,my name is unix.com. hello1,I am awesome. Hope you know this,hello2!,wat else hello2! hello1.txtHi Friends Hi Folks Hi Well-Wishers hello2.txtHoney Sweety Darling Expected outputHi Friends,my name is unix.com. Hi Friends,I am awesome. Hope you... (4 Replies)
Discussion started by: Makarand Dodmis
4 Replies

4. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

5. Shell Programming and Scripting

awk loop and using shell in awk

Hi, everyone! I have a file, when I print its $1 out it show several strings like this: AABBCC AEFJKLFG FALEF FAIWEHF What I want to do is that, after output of each record, search the string in all files in the same folder, print out the record and file name. This is what I want... (4 Replies)
Discussion started by: xshang
4 Replies

6. Shell Programming and Scripting

using awk loop

Hi, Gurus, I have a requirement as following: source data: 103,8,25 I want to get 103 8 25 1_8 103 8 25 9_16 103 8 25 17_24 103 8 25 25_32 103 8 25 33_40 103 8 25 41_48 103 8 25 49_56 103 8 25 57_64 103 8 25 65_72 103 8 25 73_80 103 8 25 81_88 103 8 25 89_96 103 8 25 97_104 103 8... (5 Replies)
Discussion started by: ken002
5 Replies

7. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

8. Shell Programming and Scripting

Using AWK in a for loop

Hello, I am trying to use AWK to print only the first field of numerous text files, and then overwrite these files. They are of the format 1*2,3,4,5. I have tried the following code (using tcsh): foreach f (file1 file2 file3) cat $f | awk -F'*' '{print $1}' > $f end However, I get very... (4 Replies)
Discussion started by: Jahn
4 Replies

9. Programming

pick the bug the server enters an infinite loop

here is the server and client side code now there is a bug after which the server enters an infinite loop.the server is designed as an echo server and if it reads /q then the server closes while the client can send messages till /q now after the frst msg when another msg is send infinite loop is... (3 Replies)
Discussion started by: arjunjag
3 Replies
Login or Register to Ask a Question