Egrep a number greater than in a column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Egrep a number greater than in a column
# 1  
Old 05-06-2013
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:

Code:
|        12 |         19 |         2000 |     9029333 | 2013-05-01_04:15:55 |   291.00h |        0 |    T |        0 | Mailbox.1113      |

my code is below (im sure this is wrong):

Code:
egrep Mailbox data | egrep "$2" | egrep "[2][0]+"

# 2  
Old 05-06-2013
Sorry, but grep is not an option for this. The grep family of utilities doesn't do arithmetic (including numeric comparisons), it matches strings.

------------------------

I take it back. There is a way to do it, it is just messy (and you will need to re-engineer a different expression for each value you're trying to compare). What do you mean by field 2? In you example is field 2 supposed to be 12 or 19? Will you ever have negative numbers? For non-zero values, will there ever be a leading 0?

With grep you don't have $2 to specify the second field and once you have the second field you don't have >.

Last edited by Don Cragun; 05-06-2013 at 02:51 PM.. Reason: Need more details.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 05-06-2013
grep does not have a way to tokenise the input like awk/perl (it's not built to do so). You can cook a pattern to match all the way until the required field, but then.. you have awk/perl.

To check if number in field #2 > 10 :
Code:
grep Mailbox datafile | egrep "^\| *[0-9]+ *\| *1[0-9]+"

This User Gave Thanks to balajesuri For This Post:
# 4  
Old 05-06-2013
Just a suggestion: if awk is not an option, how about using bash script?
Code:
#!/bin/bash
while IFS="|" read f1 f2 skip
do
        if [[ "$skip" =~ "Mailbox" ]] && [ $f2 -gt 10 ]
        then
                echo "$f1 $f2 $skip"
        fi
done < data

This User Gave Thanks to Yoda For This Post:
# 5  
Old 05-06-2013
Quote:
Originally Posted by balajesuri
grep does not have a way to tokenise the input like awk/perl (it's not built to do so). You can cook a pattern to match all the way until the required field, but then.. you have awk/perl.

To check if number in field #2 > 10 :
Code:
grep Mailbox datafile | egrep "^\| *[0-9]+ *\| *1[0-9]+"

looks like this could be the answer. although it would be dirty.

in this case, the bolded would be considered the second column/field:

| 12 | 19 | 2000 | 9029333 | 2013-05-01_04:15:55 | 291.00h | 0 | T | 0 | Mailbox.1113 |

---------- Post updated at 02:04 PM ---------- Previous update was at 01:46 PM ----------

sorry guys, i'm having issues with this:

i want to show whichever line has a value of 5 or more. i'm trying to use this:

Code:
egrep "^\| *[0-9]+ *\| *[5-9]+" data

but it doesn't seem to be working.

---------- Post updated at 02:10 PM ---------- Previous update was at 02:04 PM ----------

Quote:
Originally Posted by Don Cragun
Sorry, but grep is not an option for this. The grep family of utilities doesn't do arithmetic (including numeric comparisons), it matches strings.

------------------------

I take it back. There is a way to do it, it is just messy (and you will need to re-engineer a different expression for each value you're trying to compare). What do you mean by field 2? In you example is field 2 supposed to be 12 or 19? Will you ever have negative numbers? For non-zero values, will there ever be a leading 0?

With grep you don't have $2 to specify the second field and once you have the second field you don't have >.
In this example, the 2nd field would be 19. there's not gonna be a negative number.
# 6  
Old 05-06-2013
To match $3 >= 5, followed by "Mailbox" when your field terminator is '|' try:
Code:
egrep '^[|][^|]*[|] *0*([5-9]|[1-9][0-9]).*Mailbox' data

After finding the 2nd "|" on the line it ignores any number of spaces, any number of leading zeros, and then it looks for a single digit that is 5 through 9 or two or more digits starting with 1 through 9.

To match $3 > 20, followed by "Mailbox" try:
Code:
egrep '^[|][^|]*[|] *0*(2[1-9]|[3-9][0-9]|[1-9][0-9]{2}).*Mailbox' data

After finding the 2nd "|" on the line it ignores any number of spaces, any number of leading zeros, and then looks for a two digit string that is 21-29, a two digit string that is 30-99, or a 3 or more digit string that is greater than or equal to 100.

As you can see you'll need to craft the ERE to use to select the numeric values you're trying to match. Hope these examples help.

Last edited by Don Cragun; 05-06-2013 at 05:30 PM.. Reason: fixed tag
# 7  
Old 05-06-2013
Here a way to do it:
Code:
$ cat infile
|        6 |         21 |         2000 |     9029333 | 2013-05-01_04:15:55 |   291.00h |        0 |    T |        0 | Mailbox.1113      |
|        5 |         20 |         2000 |     9029333 | 2013-05-01_04:15:55 |   291.00h |        0 |    T |        0 | Mailbox.1113      |
|        4 |         19 |         2000 |     9029333 | 2013-05-01_04:15:55 |   291.00h |        0 |    T |        0 | Mailbox.1113      |

Code:
$ cat test.sh
echo Lines with first field GE 5:
grep -e "^ *| *0*[5-9]" \
     -e "^ *| *[1-9][0-9]" infile

echo
echo Lines with second field GT 20:
grep -e "^ *| [^|]* | *2[1-9]" \
     -e "^ *| [^|]* | *[3-9][0-9]" infile

Code:
$ ./test.sh
Lines with first field GE 5:
|        6 |         21 |         2000 |     9029333 | 2013-05-01_04:15:55 |   291.00h |        0 |    T |        0 | Mailbox.1113      |
|        5 |         20 |         2000 |     9029333 | 2013-05-01_04:15:55 |   291.00h |        0 |    T |        0 | Mailbox.1113      |

Lines with second field GT 20:
|        6 |         21 |         2000 |     9029333 | 2013-05-01_04:15:55 |   291.00h |        0 |    T |        0 | Mailbox.1113      |

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 greater than number

data: 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? cat data | egrep ..... can't use awk here. i was... (7 Replies)
Discussion started by: SkySmart
7 Replies

5. Shell Programming and Scripting

awk to substitute third column if first column is greater than interest

A file 2400 2800 PSC000289 3200 3896 PCS000289 3333 3666 PCS000221 222 1000 PCS000222 3299 3600 PSC000289 Question is while if third column is PCS000289 and first column should be greater than 3000, then replace PCS000289 by YES, remaining the others column same. ... (1 Reply)
Discussion started by: cdfd123
1 Replies

6. 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

7. Shell Programming and Scripting

Greping the column with a value greater than 50 with awk

Hi All, I am running a command from a remote server using ssh to different servers. I will get a output like below with 4 columns. I want to grab line which is having a coulmn which grate than or equal to 50. How can I do it with Awk or sed ??. I add a space to first row with sed 's/::/:: /g' to... (4 Replies)
Discussion started by: raghin
4 Replies

8. 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

9. 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

10. 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
Login or Register to Ask a Question