The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Splitting a Larger File Into Mutiple Smaller ones. madhubt_1982 Shell Programming and Scripting 1 03-22-2008 10:10 AM
Cutting a large log file in to smaller ones MrTangent Shell Programming and Scripting 11 11-07-2007 06:55 AM
sending larger files via ftp matrixmadhan UNIX for Advanced & Expert Users 6 05-24-2006 11:50 AM
Anything smaller than sleep 1 markms UNIX for Dummies Questions & Answers 4 04-07-2006 01:22 PM
Smaller splash graphic kduffin Post Here to Contact Site Administrators and Moderators 2 11-21-2003 12:03 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-10-2007
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Grab a smaller and larger value

Hi All,

I am trying to grab a term which is just smaller and larger than the assigned value using the below code. But there seems to be some problem. The value i assign is 25 so i would expect it to output a smaller value to be 20 instead of 10 and 20 and larger value to be 30 instead of 30 and 40. Can anybody help ?

Input:

10
20
30
40


Output:
My Value = 25
Smaller Value = 20
Bigger Value = 30



Code:
awk '$1<= 25 END{print $1}' input
awk '$1>= 25 BEGIN{print $1}' input


Last edited by vgersh99; 10-10-2007 at 10:50 PM.. Reason: code tags
  #2 (permalink)  
Old 10-10-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
read up on 'man awk'

Code:
echo '20' | awk '{printf("%s Value = %d\n", ($1 < 25) ? "Smaller" : "Bigger", $1)}'

nawk -f ray.awk inputFile

ray.awk:

Code:
BEGIN {
  myValue=25
  bigger=9999999999
}
{
  if ( $1 < myValue && $1 > smaller ) smaller = $1
  if ( $1 > myValue && $1 < bigger )  bigger = $1
}
END {
   printf("My Value = %d\nSmaller Value = %d\nBigger Valuer = %d\n", myValue, smaller, bigger)
}


Last edited by vgersh99; 10-10-2007 at 11:14 PM..
  #3 (permalink)  
Old 10-11-2007
chella chella is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 75
Hi,

To grab
the smaller value : awk '{if($1<=25) {print $1;}}' input | sort | tail -1
the larger value : awk '{if($1>=25) {print $1;}}' input | sort -r | tail -1

Regards,
Chella
  #4 (permalink)  
Old 10-11-2007
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Hi vgersh99 & chella,

Thanks alot for your help!!!!!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:00 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0