BASH ZCAT EGREP Shell Script | 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.

BASH ZCAT EGREP Shell Script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-23-2012
metallica1973 metallica1973 is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 24 September 2014, 10:05 AM EDT
Location: Washington D.C
Posts: 176
Thanks: 17
Thanked 2 Times in 2 Posts
BASH ZCAT EGREP Shell Script

I created a backup script that emails all the admins when the backup is complete and attaches a log file of what what backed up. On occasion, something happens in which the backups stop working, I started "grep"ing around /var/log/syslog and I usually find the smoking gun. My goal is to zcat /var/log/syslog* and egrep all the instances of an occurence on a date that an error occurred. So I began testing using:

Grabs Date


Code:
err_date=$((date)|gawk -F ' ' 'BEGIN {IGNORECASE=1;} {print $2,$3}')
echo $err_date
Oct 23

Now I want to simply use zcat and egrep the lines that contain that date.


Code:
zcat /var/log/syslog* |egrep '^`echo $err_date`'
gzip: /var/log/syslog: not in gzip format
gzip: /var/log/syslog.1: not in gzip format

I remove the backticks


Code:
zcat /var/log/syslog* |egrep '^$err_date`'
gzip: /var/log/syslog: not in gzip format
gzip: /var/log/syslog.1: not in gzip format


Code:
zcat /var/log/syslog*|gawk 'BEGIN {IGNORECASE=1;} /^$err_date/'

I tried many combinations but cant seem to catch my error in usage. I want egrep to use "Oct 23" from variable "$err_date".

??

Last edited by metallica1973; 10-23-2012 at 01:04 PM..
Sponsored Links
    #2  
Old 10-23-2012
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 October 2014, 4:50 PM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts
Use
Code:
grep "^$err_date"

instead of
Code:
egrep '^$err_date`'

.
Sponsored Links
    #3  
Old 10-23-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 15 September 2014, 8:06 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
Quote:
Originally Posted by metallica1973 View Post

Code:
zcat /var/log/syslog* |egrep '^`echo $err_date`'
gzip: /var/log/syslog: not in gzip format
gzip: /var/log/syslog.1: not in gzip format

The problem is highlighted in red..

Are you performing operations on .gz files..?

I normally prefer.


Code:
zcat *.gz | ....

    #4  
Old 10-23-2012
metallica1973 metallica1973 is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 24 September 2014, 10:05 AM EDT
Location: Washington D.C
Posts: 176
Thanks: 17
Thanked 2 Times in 2 Posts
Thanks for the reply but it didnt work:


Code:
zcat /var/log/syslog* |grep "^$err_date"

gzip: /var/log/syslog: not in gzip format

gzip: /var/log/syslog.1: not in gzip format

?

---------- Post updated at 01:07 PM ---------- Previous update was at 01:05 PM ----------

Thanks PAMU,

I would like to grep all files under:

/var/log/syslog*

which would include:


Code:
-rw-r----- 1 syslog adm  133256 Oct 17 08:04 /var/log/syslog.7.gz
-rw-r----- 1 syslog adm  146465 Oct 18 07:56 /var/log/syslog.6.gz
-rw-r----- 1 syslog adm  158721 Oct 19 07:48 /var/log/syslog.5.gz
-rw-r----- 1 syslog adm  172300 Oct 20 07:40 /var/log/syslog.4.gz
-rw-r----- 1 syslog adm  183107 Oct 21 07:49 /var/log/syslog.3.gz
-rw-r----- 1 syslog adm  206954 Oct 22 07:45 /var/log/syslog.2.gz
-rw-r----- 1 syslog adm 5840020 Oct 23 07:49 /var/log/syslog.1
-rw-r----- 1 syslog adm 1342525 Oct 23 13:06 /var/log/syslog

thanks
Sponsored Links
    #5  
Old 10-23-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 15 September 2014, 8:06 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
try using


Code:
zcat /var/log/syslog.*.gz | grep "^$err_date"

Sponsored Links
    #6  
Old 10-23-2012
metallica1973 metallica1973 is offline
Registered User
 
Join Date: Dec 2007
Last Activity: 24 September 2014, 10:05 AM EDT
Location: Washington D.C
Posts: 176
Thanks: 17
Thanked 2 Times in 2 Posts
If I just:


Code:
zcat -v /var/log/syslog*
Oct 17 08:04:31 Dartanion nullmailer[1572]: Starting delivery: protocol: smtp host: mail. file: 1350367203.17107
Oct 17 08:04:31 Dartanion nullmailer[9414]: smtp: Failed: Connect failed
Oct 17 08:04:31 Dartanion nullmailer[1572]: Sending failed:  Host not found
Oct 17 08:04:31 Dartanion nullmailer[1572]: Starting delivery: protocol: smtp host: mail. file: 1350108002.10660
Oct 17 08:04:31 Dartanion nullmailer[9415]: smtp: Failed: Connect failed
Oct 17 08:04:31 Dartanion nullmailer[1572]: Sending failed:  Host not found
Oct 17 08:04:31 Dartanion nullmailer[1572]: Starting delivery: protocol: smtp host: mail. file: 1350453603.30086
Oct 17 08:04:31 Dartanion nullmailer[9416]: smtp: Failed: Connect failed
Oct 17 08:04:31 Dartanion nullmailer[1572]: Sending failed:  Host not found
Oct 17 08:04:31 Dartanion nullmailer[1572]: Starting delivery: protocol: smtp host: mail. file: 1350215920.13026
Oct 17 08:04:31 Dartanion nullmailer[9417]: smtp: Failed: Connect failed
Oct 17 08:04:31 Dartanion nullmailer[1572]: Sending failed:  Host not found
Oct 17 08:04:31 Dartanion nullmailer[1572]: Delivery complete, 8 message(s) remain.
 95.8%

I can see it going through all the files and or most of the files but I cannot seem to pass my variable into grep or egrep. ?

Last edited by Franklin52; 10-24-2012 at 02:41 AM.. Reason: fixed code tags
Sponsored Links
    #7  
Old 10-23-2012
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 3 October 2014, 8:50 AM EDT
Posts: 3,059
Thanks: 33
Thanked 659 Times in 638 Posts

Code:
for i in /var/log/syslog*
do
    echo "$i" | grep "gz$" > /dev/null 2>&1 && zcat "$i" | grep "^$err_date"
    echo "$i" | grep "gz$" > /dev/null 2>&1 || grep "^$err_date" "$i"
done

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
zcat two files jacobs.smith Shell Programming and Scripting 11 10-04-2012 12:28 PM
bash: need to have egrep to return a text string if the search pattern has NOT been found bash4ever Shell Programming and Scripting 6 07-12-2012 02:54 AM
zcat on Linux felixmat1 Linux 2 06-19-2008 03:42 PM
zcat --> Arg list too long muneebr UNIX for Dummies Questions & Answers 3 08-19-2004 04:40 AM
bash shell script norsk hedensk Shell Programming and Scripting 4 08-02-2003 07:27 PM



All times are GMT -4. The time now is 11:59 PM.