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
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 :
How do i resolve it
Last edited by Franklin52; 09-30-2012 at 05:53 PM..
Reason: Please use code tags
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:
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):
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..
#!/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
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)
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)
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)
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)
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)
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)
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)
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)