How does egrep work? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

How does egrep work?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-08-2013
brownwrap brownwrap is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 27 February 2013, 3:08 PM EST
Posts: 61
Thanks: 7
Thanked 1 Time in 1 Post
How does egrep work?

I use grep all the time. Don't have a problem understanding it. But I am looking at a script that uses 'eprep'. Basically we have something that uses ssh to log into a remote host and execute a "df -h" and emails us. We use that to check space on remote machines. Lately I have noticed the 'egrep' command isn't returning anything. Someone else who is no longer around wrote this. I don't understand what the statement is doing. On one machine it returns what I would expect, on others, it returns nothing. On a working machine:

Code:
 ssh remote_machine  df -h | /usr/bin/egrep "100%|9[0-9]%|8[0-9]%|7[0-9]%|capacity"
Filesystem             size   used  avail capacity  Mounted on
/dev/md/dsk/d33         87G    65G    20G    77%    /opt
/dev/md/dsk/d34        7.9G   6.8G   983M    88%    /export/home
/opt/u01                87G    65G    20G    77%    /u01
/opt/u02                87G    65G    20G    77%    /u02
/opt/u01/u03            87G    65G    20G    77%    /u03
/u02/u04                87G    65G    20G    77%    /u04

Machine that doesn't work:


Code:
  ssh remote_host  df -h | /usr/bin/egrep "100%|9[0-9]%|8[0-9]%|7[0-9]%|capacity"
Filesystem             size   used  avail capacity  Mounted on

If I leave off the egrep, I do see the output of the df command, so I am sucessfully logging into the remote machine.
Sponsored Links
    #2  
Old 02-08-2013
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Gotham Knight
 
Join Date: Mar 2012
Last Activity: 22 August 2014, 6:14 AM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 495 Times in 472 Posts
Please post output from the second df command.
Sponsored Links
    #3  
Old 02-08-2013
brownwrap brownwrap is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 27 February 2013, 3:08 PM EST
Posts: 61
Thanks: 7
Thanked 1 Time in 1 Post
I did post the output. It simply returns the heading to a df, no content:


Code:
ssh remote_machine df -h | /usr/bin/egrep "100%|9[0-9]%|8[0-9]%|7[0-9]%|capacity"

Filesystem             size   used  avail capacity  Mounted on

    #4  
Old 02-08-2013
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Gotham Knight
 
Join Date: Mar 2012
Last Activity: 22 August 2014, 6:14 AM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 495 Times in 472 Posts
I meant the df output without egrep filtering.
And, I assume that you realise that the filter only passes through specific percentages/text from its input.
Sponsored Links
    #5  
Old 02-08-2013
brownwrap brownwrap is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 27 February 2013, 3:08 PM EST
Posts: 61
Thanks: 7
Thanked 1 Time in 1 Post
Ah, OK. No I didn't understand that. I tried the egrep and just grepped for capacity, but didn't understand the percentages. Here is the df without the egrep. Now that you said that I am guessing it is looking for output that is 70% or above:


Code:
Filesystem             size   used  avail capacity  Mounted on
/dev/md/dsk/d30         16G   4.6G    11G    30%    /
/devices                 0K     0K     0K     0%    /devices
ctfs                     0K     0K     0K     0%    /system/contract
proc                     0K     0K     0K     0%    /proc
mnttab                   0K     0K     0K     0%    /etc/mnttab
swap                   8.1G   1.4M   8.1G     1%    /etc/svc/volatile
objfs                    0K     0K     0K     0%    /system/object
sharefs                  0K     0K     0K     0%    /etc/dfs/sharetab
/platform/SUNW,T5140/lib/libc_psr/libc_psr_hwcap2.so.1    16G   4.6G    11G    30%    /platform/sun4v/lib/libc_psr.so.1
/platform/SUNW,T5140/lib/sparcv9/libc_psr/libc_psr_hwcap2.so.1    16G   4.6G    11G    30%    /platform/sun4v/lib/sparcv9/libc_psr.so.1
fd                       0K     0K     0K     0%    /dev/fd
/dev/md/dsk/d32         16G   5.8G    10G    37%    /var
swap                   8.1G   8.3M   8.1G     1%    /tmp
swap                   8.1G    48K   8.1G     1%    /var/run
/dev/md/dsk/d33         87G    47G    39G    55%    /opt
/dev/md/dsk/d34        7.9G   4.7G   3.1G    61%    /export/home
/opt/u01                87G    47G    39G    55%    /u01
/opt/u02                87G    47G    39G    55%    /u02
/opt/u01/u03            87G    47G    39G    55%    /u03

Sponsored Links
    #6  
Old 02-08-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 22 August 2014, 9:36 AM EDT
Location: Simplicity
Posts: 1,843
Thanks: 117
Thanked 532 Times in 482 Posts
The "|" in egrep is an "or".
It problably make more sense to convert to

Code:
ssh remote_machine df -h | awk 'NR==1 {header=$0 RS} $5~/[7-9][0-9]%|100%/ {print header $0; header=""}'

awk takes the same ERE as egrep, namely "[7-9][0-9]%|100%" but this operates on $5=column#5 only.
The main advantage is that it only prints the header (from NR=line#1) if needed.
The Following User Says Thank You to MadeInGermany For This Useful Post:
brownwrap (02-08-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
My script work on Linux but not work in sunos. ooilinlove Shell Programming and Scripting 7 01-30-2013 06:27 AM
if [ -z echo foo | egrep -e 'regexp' != '' ] -> dont work wiseguy Shell Programming and Scripting 1 01-27-2009 08:46 AM
search ")" with egrep - egrep: syntax error sagarjani UNIX for Dummies Questions & Answers 7 10-14-2008 07:30 AM
Egrep cheat sheet anywhere? Looking for meaning of egrep -c leelm UNIX for Dummies Questions & Answers 2 01-11-2008 02:37 PM
Script doesn't work, but commands inside work cheongww UNIX for Dummies Questions & Answers 2 11-14-2006 09:52 PM



All times are GMT -4. The time now is 01:41 PM.