on command prompt when i am testing I am able to get the value of 'msgtime' parameter but in the script I get a space value for it . Please help ..
following is the code snippet -
grep "Component Manager stopped" msgtime.txt | while read line
do
awk '{ print $2}' | sed 's/://g' | read msgtime
echo $msgtime # does not display msgtime instead gives space
done
msgtime.txt looks like this -
pe.txt.2012-03-28:2012/03/28 11:51:02 [RMI TCP Connection(147)-10.87.130.42] INFO filenet.vw.ComponentIntegrator Component Manager stopped.
1st error was that ... | read msgtime invokes read in a subshell and therefore the value of msgtime is not accessible outside of that. (edit: well, not that it'd work without an input to awk)
This error is that you cannot put spaces around the = when doing an assignment. msgtime=`echo ...
itkamaraj's solution is a great one, or do you need to capture it and do more processing with $msgtime ?
awk '/Component Manager stopped/ {gsub(":","",$2);print $2}' msgtime.txt | while read msgtime
do
echo $msgtime
...
done
awk '/Component Manager stopped/ {gsub(":","",$2);print $2}' msgtime.txt | while read msgtime do # I was using msgtime elsewhere in script but I included it here and its working.. :-) done
Thanks a tonnes..................
---------- Post updated at 01:24 PM ---------- Previous update was at 12:20 PM ----------
Guys , stuck a bit ..
my log file pe.txt is a huge log file , inorder to get the latest alert I read from tthe bottom
How can I give use awk here to read from the bottom of file and execute -
tail -r pe.txt | awk '/Component Manager stopped/ {gsub(":","",$2);print $2}' | while read msgtime
do
test $temp -eq $msgtime
if [ $? -eq 0 ]; then
echo $msgtime
echo "tivoli already generated"
else
echo "$msgtime" > newfile.txt
touch ComponentManagerDown_on_`hostname`.txt
fi
done
Shailesh,
My requirement is to read a log file (pe.txt) of 35kB and send an alert if any string "Component Manager stopped" is received. also not to report the same alert again.
So ,I am parsing file from bottom to top to get latest alert.
with the code I posted I get following output which is not correct
[output]
151617
tivoli already generated
151617
tivoli already generated
[\output]
In code below,if pe.txt does not have a string "Component Manager stopped" then when my awk does not find the intended string in the txt file then 'read' command passes an empty value to "msgtime" parameter , I tried to put an if statement to check for an empty string still error comes shown below code..
Please suggest how should I tackle it here ..
cat newfile.txt | read temp
awk '/Component Manager stopped/ {gsub(":","",$2);last=$2}END{print last}' pe.txt | while read msgtime
do
if [$msgtime -ne null]; then
test $temp -eq $msgtime
if [ $? -eq 0 ]; then
echo $msgtime
echo "tivoli already generated"
else
echo "$msgtime" > newfile.txt
touch ComponentManagerDown_on_`hostname`.txt
fi
else
echo "timestamp is null" > /dev/null 2>&1
fi
done
running the above gives me error -
[error]
[aeadmin@FNPRODXT1:/AELogs]$ ksh tivoliscript.ksh
tivoliscript.ksh[11]: test: 0403-021 A ] character is missing.
[/error]