How to grep a pattern having value greater than 123 in a file?

Hi,

I have a dynamically growing ascii file which has large data (both text and digits). I need to grep those lines having value greater than '123'. These numeric values may appear at anywhere in the line, hence I could not use awk to split to columns.

So, please help me with the grep regular expression pattern for this.

Try this code:

 
grep "12[4-9][0-9]*" filename

Try:

perl -ne 'print if grep {$_>123} /(\d{3,})/g' file
1 Like

Thanks, but this suggestion locks down the first 2 digits should be '12'. But I want to get any value that is greater than 123

---------- Post updated at 10:11 AM ---------- Previous update was at 10:11 AM ----------

Is that possible to do this using only 'grep'?

This should work provided if you have the number seperated by space

grep -v -e " [0-9] " -e " [0-9][0-9] " -e " 12[0-3] " filename 

I am trying to eliminate all one,two,three digit numbers but greater than 123

But, this fails suppose if the file is having a line like this:
"rdate NTPserver login 115 /var/log".

This line is not supposed to be shown in the output as 115 < 123. But, it is still being shown :frowning:

# echo "rdate NTPserver login 140 /var/log"|sed '/\b0*[0-9]\b\|\b0*[0-9][0-9]\b\|0*1[1-2][0-3]\|0*1[0-1][0-9]/d'
rdate NTPserver login 140 /var/log
# echo "rdate NTPserver login 121 /var/log"|sed '/\b0*[0-9]\b\|\b0*[0-9][0-9]\b\|0*1[1-2][0-3]\|0*1[0-1][0-9]/d' 

regards
ygemici

1 Like

I have missed one pattern.

 for ((i=0;i<130;i++)); do echo " $i "|grep -v -e " [0-9] " -e " [0-9][0-9] " -e " 1[0-1][0-9] " -e " 12[0-3] "

 124
 125
 126
 127
 128
 129

how would you do the same thing but only for values before the first "," in each line AND for decimal numbers ie 400.232352,ATK4875739,234,2,33,3?

$
$
$ cat f18
400.232352,ATK4875739,234,2,33,3  # show, because no. from beginning to 1st comma is decimal and > 123
100.23,XYZ,0                      # don't show, because no. from beginning to 1st comma is decimal but < 123
125,ABC,0                         # don't show, because no. from beginning to 1st comma is not a decimal
12PQR,DEF,0                       # don't show, because chars from beginning to 1st comma do not represent a number
123.000001,ATK4875739,234,2,33,3  # show, because no. from beginning to 1st comma is decimal and > 123
-100.23,MNO,0                     # don't show, because no. from beginning to 1st comma is decimal but < 123
$
$
$ perl -lne '/^(.*?),.*/; print if $1 !~ /[a-zA-Z]/ and int($1) != $1 and $1 > 123' f18
400.232352,ATK4875739,234,2,33,3  # show, because no. from beginning to 1st comma is decimal and > 123
123.000001,ATK4875739,234,2,33,3  # show, because no. from beginning to 1st comma is decimal and > 123
$
$
$

tyler_durden

Thanks kumaran_5555 for this. But, why this works only when there is a space before and after the pattern?

awk -F, '/,/{if($1>123){print}next}{x=$0;gsub(/[^0-9]/,"");if($0+0>123){print x} }' inputFile

Something like this?

$ cat file
rdate NTPserver login 115 /var/log
rdate NTPserver login 123.4 /var/log
rdate NTPserver login 123 /var/log
rdate NTPserver login 124 /var/log
400.232352,ATK4875739,234,2,33,3
110.232352,ATK4875739,234,2,33,3

$ awk -F, '/,/{if($1>123){print}next}{x=$0;gsub(/[^0-9]/,"");if($0+0>123){print x} }' file
rdate NTPserver login 123.4 /var/log
rdate NTPserver login 124 /var/log
400.232352,ATK4875739,234,2,33,3

--ahamed

1 Like

hi,

if you have gawk installed, this seems to work :

echo "rdate NTPserver login 140 /var/log
rdate NTPserver login 121 /var/log" | \
   awk '{var=$0; var=gensub(/[^[:digit:]*]/,"","g",var); if ( var > 123 ) { print $0}}'
1 Like