Egrep a greater than number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Egrep a greater than number
# 1  
Old 04-05-2013
Egrep a greater than number

data:
Code:
hello mr smith 400 you all ok?
hello mrs. smith 700 you all ok?
hello mr. everyone 150 you all ok?
hello mr. you all 199 im lad you are ok


using egrep, how can i grep out only lines that have a number greater than 250?

Code:
cat data | egrep .....

can't use awk here. i was wondering if this is possible with egrep?

the numbers can be in any column, but each line will always have "hello mr.*you all ok"

System OS: Linux/SunOS
# 2  
Old 04-05-2013
Yes, it's possible:
Code:
$ cat data
hello mrs. smith 700 you all ok?
hello mr. everyone 250
hello mr. everyone 251
hello mr. everyone 4150
hello mr. you all 249 im lad you are ok

Code:
$ egrep "2[5-9][0-9]|[3-9][0-9][0-9]|[1-9][0-9][0-9][0-9]" data
hello mrs. smith 700 you all ok?
hello mr. everyone 251
hello mr. everyone 4150

This does not take into account negative numbers...

Last edited by hanson44; 04-05-2013 at 06:13 PM.. Reason: oops - change 1 to 0
This User Gave Thanks to hanson44 For This Post:
# 3  
Old 04-06-2013
it appears this only works on 3 digit numbers:

data:

Code:
Webservice response: 250 OK (74 ms)
Webservice response: 900 OK (740 ms)
Webservice response: 501 OK (573 ms)
Webservice response: 407 OK (74000 ms)
Webservice response: 200 OK (501 ms)
Webservice response: 503 OK (74 ms)
Webservice response: 201 OK (74 ms)
Webservice response: 800 OK (677 ms)
Webservice response: 200 OK (74 ms)
Webservice response: 250 OK (500 ms)
Webservice response: 100 OK (514 ms)
Webservice response: 501 OK (145 ms)

Code:
egrep '\([5-9][1-9][0-9] ms\)|\([5-9][0-9][1-9] ms\)|\([5-9][0-9][1-9]\.* ms\)' data

Webservice response: 900 OK (740 ms)
Webservice response: 501 OK (573 ms)
Webservice response: 200 OK (501 ms)
Webservice response: 800 OK (677 ms)
Webservice response: 100 OK (514 ms)

notice it didn't grab this line even though it should have:

Code:
Webservice response: 407 OK (74000 ms)

# 4  
Old 04-06-2013
Quote:
Originally Posted by SkySmart
it appears this only works on 3 digit numbers:

data:

Code:
Webservice response: 250 OK (74 ms)
Webservice response: 900 OK (740 ms)
Webservice response: 501 OK (573 ms)
Webservice response: 407 OK (74000 ms)
Webservice response: 200 OK (501 ms)
Webservice response: 503 OK (74 ms)
Webservice response: 201 OK (74 ms)
Webservice response: 800 OK (677 ms)
Webservice response: 200 OK (74 ms)
Webservice response: 250 OK (500 ms)
Webservice response: 100 OK (514 ms)
Webservice response: 501 OK (145 ms)

Code:
egrep '\([5-9][1-9][0-9] ms\)|\([5-9][0-9][1-9] ms\)|\([5-9][0-9][1-9]\.* ms\)' data

Webservice response: 900 OK (740 ms)
Webservice response: 501 OK (573 ms)
Webservice response: 200 OK (501 ms)
Webservice response: 800 OK (677 ms)
Webservice response: 100 OK (514 ms)

notice it didn't grab this line even though it should have:

Code:
Webservice response: 407 OK (74000 ms)

Notice that the egrep command that hanson44 gave you would have matched both 407 and 74000 on that line.

This is very different from your original request. If what you now want is to select lines with numbers greater than or equal to 500 immediately followed by the string " ms" enclosed in parentheses, that could be done with something like:
Code:
egrep '\(([5-9][0-9]|[1-9][0-9][0-9])[0-9]+ ms\)' data

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 04-06-2013
On the web site, I re-edited the original syntax, to correct a typo. If you get this by email, you probably did not get that correction. Here is the correct form:
Code:
 $ egrep "2[5-9][0-9]|[3-9][0-9][0-9]|[1-9][0-9][0-9][0-9]" data

It changes [1-9] to [0-9] in two places, and actually changes the logic to ">= 250". We could switch 2[5-9][0-9]| to 25[1-9]|2[6-9][0-9] to get > 250 logic.

So it's saying 250-299 OR 300-999 OR 1000-9999
--------------------

I modified the test file a little, to make it easier to tell if working:
Code:
$ cat data
Webservice response: 201 OK (74 ms)
Webservice response: 201 OK (250 ms)
Webservice response: 201 OK (251 ms)
Webservice response: 501 OK (573 ms)
Webservice response: 407 OK (7400 ms)
Webservice response: 407 OK (74000 ms)
Webservice response: 407 OK (740000 ms)

With the typo corrected, and adding the "ms" part, it does find the 74000 number, but does not find 74000, because adding "ms" to the regular expression "ties down" one end of the RE.
Code:
$ egrep "2[5-9][0-9] ms|[3-9][0-9][0-9] ms|[1-9][0-9][0-9][0-9] ms" data
Webservice response: 201 OK (250 ms)
Webservice response: 201 OK (251 ms)
Webservice response: 501 OK (573 ms)
Webservice response: 407 OK (7400 ms)
Webservice response: 407 OK (74000 ms)

With the "ms" part is included, the RE could be modified as follows (add +) to work correctly in all cases:
Code:
$ egrep "2[5-9][0-9] ms|[3-9][0-9][0-9] ms|[1-9][0-9][0-9]+[0-9] ms" data
Webservice response: 201 OK (250 ms)
Webservice response: 201 OK (251 ms)
Webservice response: 501 OK (573 ms)
Webservice response: 407 OK (7400 ms)
Webservice response: 407 OK (74000 ms)
Webservice response: 407 OK (740000 ms)

Now the RE is saying "250-299 ms" OR "300-999 ms" OR "1000-9999 ms" OR "10000-99999 ms", etc. etc. etc.
This User Gave Thanks to hanson44 For This Post:
# 6  
Old 04-06-2013
As Don Cragun said, you changed targets without saying a word. Assuming you want to find anything >= (500 ms), try also (using "bounds"):
Code:
$ grep -E '\(([5-9][0-9]{2}|[1-9][0-9]{3,}) ms\)' file
Webservice response: 900 OK (740 ms)
Webservice response: 501 OK (573 ms)
Webservice response: 407 OK (74000 ms)
Webservice response: 200 OK (501 ms)
Webservice response: 800 OK (677 ms)
Webservice response: 250 OK (500 ms)
Webservice response: 100 OK (514 ms)

This User Gave Thanks to RudiC For This Post:
# 7  
Old 04-06-2013
Why can't you use awk? It's so easy
Code:
awk '{for(i=1;i<=NF;i++) if($i+0>250) {print;break}}' file

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Greater than specific number

please let me know how to construct if then else by comparing two numbers if it is greater than 10000. I need to do some specific task executed. can you help me out in shell scripting plz. (6 Replies)
Discussion started by: ramkumar15
6 Replies

2. UNIX for Dummies Questions & Answers

Grep SQL output file for greater than number.

Hi, This is my first post. I have a korn shell script which outputs a select statment to a file. There is only one column and one row which contains a record count of the select statement. The select statement looks something like this: SELECT COUNT(some_field) AS "count_value" ... (2 Replies)
Discussion started by: MurdocUK
2 Replies

3. Shell Programming and Scripting

Grep lines for number greater than given number

Hello, I am newbie to bash scripting. Could someone help me with the following. I have log file with output as shown below **************************LOG************************* 11/20/2013 9:11:23.64 Pinging xx.xx.xx.xx with 32 bytes of data: 11/20/2013 9:11:23.64 Reply from xx.xx.xx.xx:... (4 Replies)
Discussion started by: meena_2013
4 Replies

4. Shell Programming and Scripting

Egrep a number greater than in a column

i'm aware awk can do what i'm trying to do here. but i cant use awk in this scenario given the circumstance of this box. but i need to check if a number is a certain column is over a certain value, say for instance, 20. data: | 12 | 19 | 2000 | 9029333 |... (11 Replies)
Discussion started by: SkySmart
11 Replies

5. Shell Programming and Scripting

AWK: Cannot read Number of records greater than 1(NR>1)

Hi all, I have a tab-delimited text file of size 10Mb. I am trying to count the number of lines using, grep -c . sample.txtor wc -l < sample.txt or awk 'END {print NR}' sample.txtAll these commands shows the count as 1, which means they are reading only the first header line of the file.... (3 Replies)
Discussion started by: mehar
3 Replies

6. UNIX for Dummies Questions & Answers

check if a decimal number is greater than zero

Hello, In my code I am checking to see if a variable that contains a decimal number is greater than 0 in the following manner: if do something fi However I am getting the error message (if $i for the current iteration holds 9.6352) command 9.6352 is not found How can I rectify... (5 Replies)
Discussion started by: Gussifinknottle
5 Replies

7. Shell Programming and Scripting

[Solved] Select the columns which have value greater than particular number

i have a file of the form 9488 14392 1 1.8586e-07 5702 7729 1 1.8586e-07 9048 14018 1 1.8586e-07 5992 12556 1 1.8586e-07 9488 14393 1 1.8586e-07 9048 14019 1 1.8586e-07 5992 12557 1 1.8586e-07 9488 14394 ... (1 Reply)
Discussion started by: vaibhavkorde
1 Replies

8. Shell Programming and Scripting

Show result only if number is greater then

Hello all Im trying to write one liner that will show me results only if the result of the expression is greater then 0 For example: I do : find . -name "*.dsp" | xargs grep -c SecurityHandler the result are : ./foo/blah/a.dsp:0 ./foo/blah1/b.dsp:1 ./foo/blah2/c.dsp:2... (1 Reply)
Discussion started by: umen
1 Replies

9. Shell Programming and Scripting

Finding files with names that have a real number greater then difined.

I am trying to find all files in a directory whose name has a real number larger then the number I am looking for. For example: . |-- delta.1.5.sql |-- delta.2.1.sql |-- delta.2.2.sql |-- delta.2.3.sql |-- delta.2.4.sql `-- delta.2.5.sql I know my database is at 2.2 so I want an... (2 Replies)
Discussion started by: harmonwood
2 Replies

10. Shell Programming and Scripting

[awk]: Row begins by random number and field 10 is greater than 10.00%

Hello! I wish to extract the pid where CPU is above 10% last pid: 22621; load averages: 4.71, 5.04, 5.13 15:08:34 221 processes: 212 sleeping, 2 running, 1 stopped, 6 on cpu CPU states: %... (3 Replies)
Discussion started by: Lomic
3 Replies
Login or Register to Ask a Question