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
# 8  
Old 05-06-2013
I don't understand the requirement here; with awk it's just so simple:
Code:
awk -F"|" '$3+0 >= 5 && /Mailbox/' data
awk -F"|" '$3+0 > 20 && /Mailbox/' data

Is the requirement an exit status? Then do this
Code:
awk -F"|" '$3+0 > 20 && /Mailbox/' data | grep .

or that
Code:
awk -F"|" '$3+0 > 20 && /Mailbox/ {print; found=1} END {exit 1-found}' data


Last edited by MadeInGermany; 05-06-2013 at 06:38 PM..
# 9  
Old 05-06-2013
Quote:
Originally Posted by hanson44
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      |

Note that in an ERE, | is a special character and does not match a pipe symbol. Even if that wasn't a problem, note that the 2nd grep in this script would not match 100, 026, or 040 even though all of these are >21. The OP never answered my question about whether there might be leading 0s; so I don't know if the last two matter or not. If they do matter, the 1st grep also wouldn't match 010 even though it is >=5 (assuming that the ERE was fixed to match a literal '|' rather than use it to specify a choice of EREs separated by the special character '|').
This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 05-06-2013
Quote:
The OP never answered my question
about whether there might be leading 0s
Quote:
with awk it's just so simple
I agree it's problematic, zeroes or not zeroes, and I left out 101. Yes, awk is better for this, this is a setup for awk, but the OP said could not use awk for some reason. Anyway, here's a corrected syntax:
Code:
echo Lines with first field GE 5:
grep -e "^ *| *[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
     -e "^ *| [^|]* | *1[0-9][0-9]" infile

# 11  
Old 05-07-2013
Quote:
Originally Posted by hanson44
I agree it's problematic, zeroes or not zeroes, and I left out 101. Yes, awk is better for this, this is a setup for awk, but the OP said could not use awk for some reason. Anyway, here's a corrected syntax:
Code:
echo Lines with first field GE 5:
grep -e "^ *| *[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
     -e "^ *| [^|]* | *1[0-9][0-9]" infile

Hi hanson44,
I know the OP said no awk. That is why I provided the two egrep scripts in message #5 in this thread that do what I think you're trying to do with these with the additional constraint posed by the OP that only lines containing "Mailbox" are to be printed. Am I correct in assuming that you meant to have a \ rather than infile in the spot I marked in red above?

I see that your scripts will allow leading spaces before the 1st "|" on the line that my scripts don't allow. None of the OP's samples had any lines that had leading spaces, so I don't know if this is important. Is there anything else your scripts do that the egrep scripts I provided earlier don't do correctly?

Note also that your corrected script still won't accept strings starting with three digits in the range 200-209 inclusive as being greater than 20.
# 12  
Old 05-07-2013
Yes, the final pattern must start with [1-9]. Smilie I guess this just reinforces the point that awk is so much better for this kind of thing.

Code:
echo Lines with first field GE 5:
grep -e "^ *| *[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
     -e "^ *| [^|]* | *[1-9][0-9][0-9]" infile

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