awk - updating variable in if statement

I have another question I am stuck at :wall:

I have a text file with two columns, like so...

2 0.0627279
3 0.0794451
4 0.108705
5 0.137739
6 0.190394
7 0.217407
8 0.241764
9 0.344458
10 0.460762

I'd like to go through the file line by line until the value in the second column exceeds a predefined cutoff. Then the line is written out and the cutoff value is doubled.

So say for SPACE=0.05 I want to get an output:

2 0.0627279
4 0.108705
6 0.190394
7 0.217407
9 0.344458
10 0.460762

I have...

awk 'BEGIN{space='$SPACE'}{if ($2 > space) print $0; space=space*2}' < input.dat  

"space=space*2" doesn't seem to be working. Any help fixing this?

Cheers

I'm not sure if your desired output jives with your description, but...

nawk '$2>s{print $0;s*=2}' s=0.05 myFile
1 Like

You're right, it doesn't jive... What I meant to say is "the cutoff value is successively incremented by the cutoff value". Sorry about that. I got it though, thanks to your pointers...

awk '$2>x {print $0;x=x+space}' space=0.05 x=0.05 abs.dat

Seem like I need to learn awk more systematically. Got any links to some good tutorial that doesn't take up too much time?

here's one place.
Here's another.

1 Like