unable to read a parameter

All,

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.

Please help.

you could use following:

grep "Component Manager stopped" msgtime.txt | while read line
do
msgtime=`echo $line | awk 'gsub(/:/, "",$2 );{print $2};'`
done

what is the expected output ?

you are not passing any input to awk command

---------- Post updated at 10:06 PM ---------- Previous update was at 10:04 PM ----------

 awk '/Component Manager stopped/ {gsub(":","",$2);print $2}' msgtime.txt

Hi ,

47shailesh : I get the following error

./tivoliscript.ksh[19]: msgtime:  not found.

Please suggest.

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
1 Like

Guys ,

You are all awesome .. hats off..

I used this

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.................. :slight_smile:

---------- 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

The above is not working. :frowning:

what error you are getting ?

1 Like

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]

Please Help .

To get the last one,

you can use the below awk

 
awk '/Component Manager stopped/ {gsub(":","",$2);last=$2}END{print last}' pe.txt

it will print the last time

1 Like

Hi ,

I get the intended result , however I also get this output -
[output]
./tivoliscript.ksh[22]: ^[^[[A^[[A^[d: not found.
[/output]

Please help.:rolleyes:

Issue resolved.

I was using

sed 's/://g' newfile.txt | read temp

at the start of my script which was giving output

./tivoliscript.ksh[22]: ^[^[[A^[[A^[d:  not found.

I removed the above code since I was actually not generating newfile.txt to have any colon based data...all data it has is place 6 digits.. :slight_smile: ..

Thanks Everyone. !!

Hi All,

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]

Eagerly awaiting your valuable replies.

cat newfile.txt | read temp

awk '/Component Manager stopped/ {gsub(":","",$2);last=$2}END{print last}' pe.txt | while read msgtime
                                                                                        do
                                                                                                if [ ! -z "$msgtime" ]; 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

$temp won't be set for comparison. you'd need to set it without using a pipe.

temp=$(cat newfile.txt)
or
read temp < newfile.txt

It will be set, since ksh runs the final command of a pipeline in the current shell environment. Although ksh is the exception rather than the rule.

Regards,
Alister

P.S. Hi there, methyl, Scrutinizer, Corona :wink:

my apologizes then. the "ksh" i've been using is flawed then.

$ mksh -c 'echo foo | read temp; echo "$temp"'

$ ksh93 -c  'echo foo | read temp; echo "$temp"'
foo

grr :wink:

1 Like

Seems mksh behaves like bash (and bourne sh and dash and probably others) in that respect. Both ways are allowed by POSIX, but it's not very ksh-like.

Thank you for the heads up.

Regards,
Alister