Script to check logs


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to check logs
# 1  
Old 09-27-2012
Script to check logs

I have 5 log files under different directores . say for eg abc under /home/dir1 , xyz under home/dir2 . is there a script that i can run from say /home that searchers all these files for string or combination of strings and write to a file

eg search file by timestamp|keyword
o/p in a file
# 2  
Old 09-27-2012
try something like this..

Code:
for i in /dir1/ /dir2/ /dir3/
do
grep "search_string" "$i"/file_name >> /path/output_file
done

# 3  
Old 09-29-2012
Hi Thanks for the reply .
I'm facing a new problem .
In the below snippet :

echo "enter search string"
read Searchstring

the searchstring is a date of format Sep 29 00:00:10 . when i'm giving the input it is not considering it as a single field . how do i resolve this
# 4  
Old 09-30-2012
you can directly pass the variable which having spaces.
for precaution purpose use quotes around it..

Code:
$ cat test.sh
echo "enter search string"
read Searchstring
echo "$Searchstring"

$ sh test.sh
enter search string
Sep 29 00:00:10
Sep 29 00:00:10

# 5  
Old 09-30-2012
Here is my script :
#!/bin/ksh
#
# Start of main processing
#
echo "enter String One";
read SearchString1
echo "Enter String two";
read SearchString2
echo $SearchString1
for i in `cat Loginput`;
do
if [ "$SearchString2" == '' ];then
cat $i|grep $SearchString1 >>"Logoutput"
else
cat $i|grep $SearchString1|grep $SearchString2 >>"Logoutput"
fi
done
exit 0

I'm getting the below error :
Code:
Searchstring2 = Sep 29 00:00:10
error :grep: 0652-033 Cannot open 29.
grep: 0652-033 Cannot open 00:00:10

How do i resolve it

Last edited by Franklin52; 09-30-2012 at 05:53 PM.. Reason: Please use code tags
# 6  
Old 09-30-2012
You need to quote "$SearchString1" to prevent word splitting when the variable is expanded. Think of it this way: if you typed in "grep Sep 29" it would think you were trying to look for the string "Sep" in the file "29." So when the shell expands $SearchString1, it is as if you had typed in "grep Sep 29" without any quotes. By quoting, you prevent the shell from splitting the argument into two words.

EDIT: There are a number of other problems, or at least inefficiencies in your script, but as a novice scripter myself, I understand that sometimes you just struggle through it to get the job done. Some things to consider:

1) Consider whitespace when writing scripts. Your script with meaningful whitespace:

Code:
#!/bin/ksh
# Start of main processing

echo "enter String One";
read SearchString1
echo "Enter String two";
read SearchString2
echo "$SearchString1"
for i in `cat ~/Desktop/Loginput`;
	do
		if [ "$SearchString2" == '' ]
			then
				cat $i|grep "$SearchString1" >>"Logoutput"
			else
				cat $i|grep "$SearchString1"|grep $SearchString2 >>"Logoutput"
		fi
	done
exit 0

I did move the "then" statement to its own line. While the whitespace means nothing to the shell, it makes it much easier for humans to read because you can see the organizational structure of your script.

2) ";" is not needed at the end of a line. The shell interprets a newline as the end of a command, unless it is quoted, in which case it is taken literally (example below):
Code:
echo "
"

3) It looks like you are testing whether the second search string is blank. An easier way to do this is simply [[ "$SearchString2" ]]. If the string is null, the test will return false, and the if command will execute the "else" portion of the construct.

Last edited by nextyoyoma; 09-30-2012 at 01:47 AM..
# 7  
Old 09-30-2012
Quote:
Originally Posted by Nevergivup
#!/bin/ksh
#
# Start of main processing
#
echo "enter String One";
read SearchString1
echo "Enter String two";
read SearchString2
echo $SearchString1
#for i in `cat Loginput`; # use while loop instead of for...
while read i
do
if [[ "$SearchString2" == '' ]];then # where is the condition in if..?
grep "$SearchString1" "$i" >>Logoutput # don.t need quotes around Logoutput
else
grep "$SearchString1" "$i" | grep "$SearchString2" >>Logoutput
fi
done<Loginput
exit 0
try now.. just put condition in if..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If I ran perl script again,old logs should move with today date and new logs should generate.

Appreciate help for the below issue. Im using below code.....I dont want to attach the logs when I ran the perl twice...I just want to take backup with today date and generate new logs...What I need to do for the below scirpt.............. 1)if logs exist it should move the logs with extention... (1 Reply)
Discussion started by: Sanjeev G
1 Replies

2. Shell Programming and Scripting

To check if a file is open and in use (logs are being written to it)

Hello Experts, I need to write a shell script to check if a file is open and something is being written to it. I want to know how OS handles it. I checked with lsof command but it is not working. For a test I did this. while true; do echo `date` >>abc.txt; done then I checked lsof |... (5 Replies)
Discussion started by: shekhar_4_u
5 Replies

3. Shell Programming and Scripting

Script to check response time from nginx logs

Hi, My goal is to monitor the response time from the access logs of nginx server. I am using gawk to print the needed fields - 'response time' and 'name of the service' from nginx logs. Command: gawk '($6 ~ /cloudservice/) {print $10, $6}' access.log Output: 0.645 /nc/cloudservice... (6 Replies)
Discussion started by: nshah11
6 Replies

4. Shell Programming and Scripting

Any utility or tool to check logs

HI, In our project daily we need to check for some errors in around 45-50 folders. Please let me know if there is any utility tool using which 1 can check each folder and file for error. Use: Monday we are checking if there were any error in files generated on Monday Tuesday to Wednesday we... (7 Replies)
Discussion started by: ankush_mehra
7 Replies

5. Shell Programming and Scripting

Need to check logs

I have nearly 25+ tail commands which we need to verify the logs if there is any errors on current or previous date with time. I need this to be automate and send email to me with details. Please help me on this. (5 Replies)
Discussion started by: Nasir HussainSM
5 Replies

6. UNIX for Dummies Questions & Answers

Check for updation/error/stuck of logs

Hi All, I'm a newbie in Linux Programming.:) Got some 500 processes running and I have around 20-30 logs updating for every 2mins on a server. The logs which i'm referring usually contains book name,run ids(not PID's),process name etc etc. I'm interested in finding out whether some particular... (1 Reply)
Discussion started by: Nand Kishor
1 Replies

7. Solaris

archive logs mount point space check script

I have the below shell script which is checking /archlog mount point space on cappire(solaris 10) server. When the space usage is above 80% it should e-mail. When i tested this script it is working as expected. -------------------------------------------------------------------------... (0 Replies)
Discussion started by: dreams5617
0 Replies

8. Solaris

logs to check

Hi all i want to know what are the logs we need to check when the server is down and how to resolve to make server UP? please help me with this (8 Replies)
Discussion started by: vkav
8 Replies

9. Shell Programming and Scripting

How to check whether logs are updating or not?

how to check whether logs are updating or not in unix is there any built in command or function ? (1 Reply)
Discussion started by: mail2sant
1 Replies

10. UNIX for Dummies Questions & Answers

is it possible to check logs in UNIX who deleted the files?

Hello, is it possible to check logs in UNIX who deleted the files? Is there logs in UNIX besides .sh_history? (1 Reply)
Discussion started by: james_falco
1 Replies
Login or Register to Ask a Question