awk - updating variable in if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - updating variable in if statement
# 1  
Old 11-08-2011
awk - updating variable in if statement

I have another question I am stuck at Smilie

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...

Code:
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
# 2  
Old 11-08-2011
I'm not sure if your desired output jives with your description, but...
Code:
nawk '$2>s{print $0;s*=2}' s=0.05 myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 11-08-2011
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...

Code:
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?
# 4  
Old 11-08-2011
here's one place.
Here's another.
This User Gave Thanks to vgersh99 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Updating a variable within a variable

I am trying to increment a counter variable that sits within a seperate text variable. I am initializing the counter variable once only at the start of my code. I expected to be able to increment the counter and have the text variable (that countains the counter) display as incremented throughout... (8 Replies)
Discussion started by: Flip-Flop
8 Replies

2. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

3. Shell Programming and Scripting

Variable IF statement in awk

I have multiple requirements to run data extracts (with the same basic requriement / resulsts) however the parameters of the extracts change with each request. To help make life easier I am writing a script where the parameters for the specific request are set at the start and then used as needed.... (6 Replies)
Discussion started by: meike
6 Replies

4. Shell Programming and Scripting

error while updating rows in sql with values in array variable

Hi, I need to update rows in a table based on the values in an array variable. code is : while read line do error_msg="$(echo $line)" index=`expr $index+1` done <"logs/$ffile" rows_count=${#error_msg } i=0 while do echo "error msgs is... (2 Replies)
Discussion started by: RP09
2 Replies

5. Shell Programming and Scripting

Updating variable

Hi, I have been attempting to install source code from xcrysden with little to no luck. I have attempted to follow the instructions given on the site but made the mistake on the code I downloaded. This was pointed out to my by the developer and I have since downloaded the proper code. However... (4 Replies)
Discussion started by: Zbay
4 Replies

6. Shell Programming and Scripting

Variable is not evaluated in awk statement

Dear All, I have one problem in my script, awk statement as 1. it is not evaluate the second variable $stake but the first one $channel is being done. 2.I want to assign the whole awk statement to a variable actual_code which is not being executed in my script. #!/usr/bin/sh echo "Enter... (3 Replies)
Discussion started by: chinmayadalai
3 Replies

7. Shell Programming and Scripting

Assigning Variable to AWK statement

Hi, The following command runs on in the Korn shell prompt. however i want to output the value of this to a variable. Can anyone provide a solution? echo 'ABC,DEF,"G,HI,J",KLM,"MNi,O"'| awk -F "\"" '{for(i=1;i<=NF;i++){if(i%2)gsub("\,","~^~",$i)}}1' (2 Replies)
Discussion started by: ladarlsan
2 Replies

8. Shell Programming and Scripting

If statement in awk with external variable

So I have a if statement inside an awk to check if $2 of a awk equals a specific IP but the test fails. So here is what I have. # !/bin/sh echo "Enter client ID" read ID echo "Enter month (01, 02, 03)" read month echo "Enter day (03, 15)" read day echo "Enter Year (07, 08)" read... (6 Replies)
Discussion started by: doublejz
6 Replies

9. Shell Programming and Scripting

awk updating one file with another, comparing, updating

Hello, I read and search through this wonderful forum and tried different approaches but it seems I lack some knowledge and neurones ^^ Here is what I'm trying to achieve : file1: test filea 3495; test fileb 4578; test filec 7689; test filey 9978; test filez 12300; file2: test filea... (11 Replies)
Discussion started by: mecano
11 Replies

10. UNIX for Advanced & Expert Users

AutoSys (updating global variable)

Hello all, I am very new at both AutoSys and VBScript. I have looked at the manual for hours however I just can't figure it out, the following is the problem: NOTE: AutoSys is being used in all Windows environment. But realizing that this is a UNIX forum, please do provide the answer however... (1 Reply)
Discussion started by: mik357
1 Replies
Login or Register to Ask a Question