Urgent: Help on grep the nearest value using bash script

Reply

Join Date: Dec 2007
Posts: 11
Reputation: ahjiefreak is an unknown quantity at this point 
Solved Threads: 1
ahjiefreak ahjiefreak is offline Offline
Newbie Poster

Urgent: Help on grep the nearest value using bash script

 
0
  #1
Apr 2nd, 2008
Hi,

I have a list of data in text files with :-
ColA ColB ColC ColD ColE ColF
12 122 2280 30 0.012987 1.776
13 118 2280 30 0.012987 1.954
14 115 2620 30 0.011321 2.131
15 113 2654 30 0.011177 2.309
16 145 1882 25 0.013110 2.487
17 112 2817 30 0.010537 2.664
18 126 2833 30 0.010479 2.842
19 111 2833 30 0.010479 3.020
16 145 1882 25 0.013110 2.487
17 112 2817 30 0.010537 2.664

How could I grep the nearest number to <=3.000 using bash script?

Means in the end i would like to isolate numbers in ColF which last seen the value <= 3.000 which is

18 126 2833 30 0.010479 2.842


Please help. Thanks.
Last edited by ahjiefreak; Apr 2nd, 2008 at 1:48 am. Reason: Confusion of the question
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Urgent: Help on grep the nearest value using bash script

 
0
  #2
Apr 3rd, 2008
Hey there,

This was bugging me all last night, and I finally figured out what my problem was

This should do the trick for you:

Shell Scripting Syntax (Toggle Plain Text)
  1. awk 'BEGIN {var=3; highest=0}{ j = $NF;if ( j < var && j > highest ) { highest=j} } END {print highest}' YourInputFile

That gave me

2.842

from the text you pasted in this post. There's probably a more elegant solution out there, but this'll do the trick for now

Best wishes

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Urgent: Help on grep the nearest value using bash script

 
0
  #3
Apr 3rd, 2008
Actually, oops, you can change the

Shell Scripting Syntax (Toggle Plain Text)
  1. j < var

part to

Shell Scripting Syntax (Toggle Plain Text)
  1. j <= var

just noticed that

Shell Scripting Syntax (Toggle Plain Text)
  1. awk 'BEGIN {var=3; highest=0}{ j = $NF;if ( j <= var && j > highest ) { highest=j} } END {print highest}' YourInputFile

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Urgent: Help on grep the nearest value using bash script

 
0
  #4
Apr 5th, 2008
Shell Scripting Syntax (Toggle Plain Text)
  1. awk '$NF <=3 {a[NR]=$NF;c[a[NR]]=$0}
  2. END{
  3. n=asort(a,b)
  4. print c[b[n]]
  5. }' file
output:
Shell Scripting Syntax (Toggle Plain Text)
  1. # ./test.sh
  2. 18 126 2833 30 0.010479 2.842
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Urgent: Help on grep the nearest value using bash script

 
0
  #5
Apr 5th, 2008
I knew there was a better answer out there

GhostDog74, you are an awk master! If you don't mind me posting this here, could you point me to a good awk tutorial on the web. I'd like to better understand a lot of programmatical aspects of it.

Thanks in advance and good stuff

Cheers,

Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Urgent: Help on grep the nearest value using bash script

 
0
  #6
Apr 5th, 2008
Originally Posted by eggi View Post

GhostDog74, you are an awk master!
no i am not. there are many who are better


If you don't mind me posting this here, could you point me to a good awk tutorial on the web. I'd like to better understand a lot of programmatical aspects of it.
this is where i learn AWK/SED. Also grymoire is worth a look
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Urgent: Help on grep the nearest value using bash script

 
0
  #7
Apr 6th, 2008
Cool,

Thank you very much I'll definitely check out these links (already adding them to my favorites).

You're very humble and come up with some excellent answers. I'm sure I'll see more in the future and I look forward to it

Thanks, again!

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC