Click to See Complete Forum and Search --> : GREP negative numbers
OliverW
07-09-2001, 05:48 PM
I have a couple of textfiles which contain number between -3 to -24. What would be the appropriate way to get all numbers between -15 and above (so -16, -17 etc etc) excluding everything between -3 and -14???
Hope anyone can give me a hint...
Strike
07-09-2001, 05:51 PM
dunno if grep can do ranges, but you can do it in a big for loop in bash:
for i in -15 -16 -17 -18 -19 -20 -21 -22 -23 -24; do
grep "$i" myfiles*
done
There's probably a better way of doing that range, but that should work.
OliverW
07-09-2001, 05:55 PM
Mmm is there a way just to pick -15 and above (or below, depends on how you see it). Cause it's not certain that I have -24 -27 or -21, I have fairly random numbers betwen -3 and -30 of which I only need the numbers between -15 and -30
Next to that I'm curious on how to use that shell script on my textfiles...
Strike
07-09-2001, 07:03 PM
just make a list up to the lowest number you need
you can type that in at the command line if you want
nuisance
07-10-2001, 12:38 AM
I haven't tried this, but would 'grep [-] <file>|grep [15-30]' work?
[ 10 July 2001: Message edited by: nuisance ]
The Kooman
07-12-2001, 01:23 AM
[B]Assumption[B]: You're dealing with only (1 or) 2 digit numbers.
~. grep '\(-1[5-9]\)\|\(-[2-9][0-9]\)' filename
This should catch all 2 digit (negative) numbers less than (and including) 15. i.e. from -15 to -99
HTH
--
Koo
Rob 'Feztaa' Park
07-12-2001, 08:16 AM
I'm not sure that grep supports regex that complicated all by itself. I think you have to add the -E flag to get that one running.
bdg1983
07-12-2001, 03:18 PM
one thing that may come up is that grep may try and interpret negative numbers as command line flags (like it might think you are trying to use an option -9 instead of searching for negative 9). to get it to work around this, use grep -e -9.
justlinux.com
Copyright 2007 Jupitermedia Corporation All Rights Reserved.