Grep wholenumbers / decimals


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep wholenumbers / decimals
# 1  
Old 11-23-2011
Grep wholenumbers / decimals

Hi I have a large file in which I need to search for certain whole numbers and print the whole line. I'm currently trying this via command line using grep but grep is also matching the decimal values and i just want to return the matching whole numbers (the entire line)

Example
File
Code:
ddggg    2.600    3388
hhfgf     3.510    1234
gfgfg     2.567    600
gghhd    23.57    1230

example cl
Code:
cat datafile | grep 600

This return ;
Code:
ddggg    2.600    3388
gfgfg    2.567    600

I just wanted it to return ;
Code:
gfgfg    2.567    600

Any help much appreciated. What regex do i need to use to acheive this
Cheers

Duckegg01Smilie


Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 11-24-2011 at 03:31 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 11-23-2011
Try:
Code:
awk '{for (i=1;i<=NF;i++) if ($i==600) print}' file

# 3  
Old 11-23-2011
Hi
That works, I would want this to work via a script,
my original script was simple ;
echo "enter val "
read answer
cat files | grep $answer

obviously the above did'nt get the correct results but can i substitute your fix into something similar to the above

Thanks
# 4  
Old 11-23-2011
Code:
echo "enter val "
read answer
awk -vx=$answer '{for (i=1;i<=NF;i++) if ($i==x) print}' file

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 11-23-2011
Hi

Just tried your method, it seems my shell does not like the awk options -vx the script is defined as a ksh script

error is invalid option -v

Thanks
# 6  
Old 11-23-2011
Simple example using bartus11's awk:

Code:
printf "enter value: "
read answer
nawk -v target="$answer" '{ for( i=1; i<=NF; i++ ) if( $i == target ) print; }' file

EDIT:
Crossed posts -- if you are running on a Sun (Solaris) try nawk instead of awk.
This User Gave Thanks to agama For This Post:
# 7  
Old 11-23-2011
Many thanks to both, the nawl works well on my solaris env. Just out of interest what would be the valid options for ksh using standard awk (just curiosity )
Thanks Again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Decimals reading Python

Hello, i'm new in python. Consider that i have this function that read me some data from a serial : def GetData(): line = open(serialx).read() hash = line.find("#") when = line count = line # print when, count, line return (float(when), int(count)) it gives me the result... (2 Replies)
Discussion started by: Board27
2 Replies

2. Shell Programming and Scripting

Can expr deal with decimals?

Hello, Im trying to work on a small script for a formula converting temperatures, Celsius to fahrenheit. I have a formula, and it works.... it works every way I try it. But it keeps giving me the wrong results every time with expr. The formula for C to F would be: divide by 5, multiply by... (12 Replies)
Discussion started by: hxdrummerxc
12 Replies

3. Shell Programming and Scripting

Round up the decimals

Hi All, I would like to do the following in the shell script 561.76 to 562 I tried using this echo 'scale=0; 749 * 75 /100 ' | bc but just returned only 561 Please help me . I appreciate your help Thanks rajeevm (13 Replies)
Discussion started by: rajeevm
13 Replies

4. UNIX for Dummies Questions & Answers

Regarding Decimals in Cshell

Hello... I am new to unix and I am wondering if in a C-shell script , Are we supposed to use only whole numbers........ for example..if a program needs to calculate the average of some numbers........ @ avg = (($1 +$2 + $3)/3)) is returning a whole number.........How can a decimal be achieved... (1 Reply)
Discussion started by: ravindra22
1 Replies

5. Shell Programming and Scripting

Decimals in TCSH

Hello, I want to run a loop with non-integer values (which I know I can't) so I've created a loop of integers and divided it by 10. However, these values are always rounded down to 1 significant figure. How do I get the script to keep and use the decimal value? My script is as follows #... (1 Reply)
Discussion started by: DFr0st
1 Replies

6. Shell Programming and Scripting

How to multiple decimals in linux

How to multiple decimals in Linux? When I do it like this i get an error message echo "$((1 * 1.1))" -bash: 1 * 1.1: syntax error in expression (error token is ".1") (4 Replies)
Discussion started by: 3junior
4 Replies

7. Shell Programming and Scripting

convert Regular decimals to Packed decimals

Hi, I am trying to find if there is a way to convert regular decimal values to Paced decimal values. I tried to find a c program but I could get a Packed converted to regular decimal not the other way round. If not unix please let me know if any other progrimming language I can use to do... (2 Replies)
Discussion started by: mgirinath
2 Replies

8. Shell Programming and Scripting

removing and rounding up decimals

Hi Experts, I have a command that gives me the output as below root@ckpgpay11core> cat sara | awk '{ sum += $1} ; END { print sum }' | awk {'print $1/90'} 8.88889 how do i remove the decimal spaces so that the figure will round itself to 9? Thanks. (3 Replies)
Discussion started by: aismann
3 Replies

9. Shell Programming and Scripting

Bourne and decimals??

I need to get 15% of the variable exer1 to be added to other exercises so far, i've got exer1=$1 aver=`expr $exer \* .15` but i keep getting an error that an integer value was expected. Is there anyway around this? (1 Reply)
Discussion started by: kdyzsa
1 Replies

10. Shell Programming and Scripting

handle decimals

Hi All, How we can handle decimals in (Float) in UNIX. a=73 b=5 c=`expr a / b` i am getting 14 but i need full 14.6 . Can any one help me pls? (1 Reply)
Discussion started by: subin_bala
1 Replies
Login or Register to Ask a Question