BASH ZCAT EGREP Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH ZCAT EGREP Shell Script
# 1  
Old 10-23-2012
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 02:04 PM..
# 2  
Old 10-23-2012
Use
Code:
grep "^$err_date"

instead of
Code:
egrep '^$err_date`'

.
# 3  
Old 10-23-2012
Quote:
Originally Posted by metallica1973
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.Smilie

Code:
zcat *.gz | ....

# 4  
Old 10-23-2012
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
# 5  
Old 10-23-2012
try using

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

# 6  
Old 10-23-2012
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 03:41 AM.. Reason: fixed code tags
# 7  
Old 10-23-2012
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash shell script help

Hi everyone, first time poster so I hope you'll be gentle with me. :o I am needing some help to make a script in bash to do the following: For each file with a name beginning with the string ff_ Extract the first line of the file (the date) Change "-" to "/" in the date entry to create a... (3 Replies)
Discussion started by: pierreDefuncto
3 Replies

2. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

3. Shell Programming and Scripting

Different behavior between bash shell and bash script for cmd

So I'm trying to pass certain json elements as env vars and use them later on in a script. Sample json: JSON='{ "Element1": "file-123456", "Element2": "Name, of, company written in, a very weird way", "Element3": "path/to/some/file.txt", }' (part of the) script: for s... (5 Replies)
Discussion started by: da1
5 Replies

4. Shell Programming and Scripting

Help with Bash shell script

Hi All, I have a script which as below #!/bin/bash for i in `cat servers` do ssh uname@$i "df -t xfs --total | grep total"; done > out.txtOutput as below -------------- total 140583991104 118622795524 21961195580 85% - total 140583991104 112888595524 27695395580 ... (4 Replies)
Discussion started by: npk
4 Replies

5. Shell Programming and Scripting

Need help with bash shell script

I need to create digit day script that takes a single numeric argument and then it should print out the day of the week using the number modulo 7 formula e.g: 0 - Sunday 6- Saturday 131 - Friday I am fairly new to unix so I don't know how to use the number modulo 7 formula. Does the script need... (3 Replies)
Discussion started by: lukefrost96
3 Replies

6. Shell Programming and Scripting

Bash shell script to check if script itself is running

hi guys we've had nagios spewing false alarm (for the umpteenth time) and finally the customer had enough so they're starting to question nagios. we had the check interval increased from 5 minutes to 2 minutes, but that's just temporary solution. I'm thinking of implementing a script on the... (8 Replies)
Discussion started by: hedkandi
8 Replies

7. Shell Programming and Scripting

bash: need to have egrep to return a text string if the search pattern has NOT been found

Hello all, after spending hours of searching the web I decided to create an account here. This is my first post and I hope one of the experts can help. I need to resolve a grep / sed / xargs / awk problem. My input file is just like this: ----------------------------------... (6 Replies)
Discussion started by: bash4ever
6 Replies

8. Shell Programming and Scripting

Bash Shell script--need help

Hi all, i am beginner to unix and trying out a shell script which does the following. i have to calculate a persons salary. his salary is read from the keyboard. he has two types of deductions. 40% as dearness allowance and 20% as house rent. i have to print the gross salary. here is the code... (5 Replies)
Discussion started by: Irishboy24
5 Replies

9. UNIX for Dummies Questions & Answers

need help with bash shell script

Hi guys! I have just started with shell programming!! I am having pronblem with variable subsitutuion. when i do egrep "*" marks this will give me the pattern match. but how can i catch the output of that result in a variable. if i say result = egrep "*" marks it gives me syntax... (2 Replies)
Discussion started by: vmtailor
2 Replies

10. Shell Programming and Scripting

bash shell script

im trying to teach my self bash shell scripting. i want to include command line arguments in my script i am writing for practice. when i run my script it dosnt make a difference what arguments there are, it just out puts the error message as if there were none. here is the check_opts () function in... (4 Replies)
Discussion started by: norsk hedensk
4 Replies
Login or Register to Ask a Question