Re: Colored timestamp (365 Views)
Reply
Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 1 of 7 (646 Views)
Accepted Solution

Colored timestamp

Hi I have the following script snippet which is used to ssh to a host and tail the logs , I need the timestamps to be colored (for the entries which are from today if possible) -

 

while read HST SERVER ENV PORT
do
    [ $(echo ${SERVER}) != ${WHATSERVER} ] && continue
   ssh -n ${HST} "tail -500 /Logs/${SERVER}_${HST%%.foobar.com}_${PORT}"
   ssh -n ${HST} "ls -ltr /Logs/${SERVER}_${HST%%.foobar.com}_${PORT}"
done < ${HSTFILE}

 Logs are of following format-

 

>>>>> | dtm=2012-05-03 15:00:00.295 PDT |

 

Thanks,

Allan.

Acclaimed Contributor
Dennis Handly
Posts: 25,281
Registered: ‎03-06-2006
Message 2 of 7 (633 Views)

Re: Colored timestamp

[ Edited ]

You might want to combine the two ssh commands into one:

ssh -n ${HST} "tail -500 /Logs/${SERVER}_${HST%%.foobar.com}_${PORT} ; \

                            ls -ltr /Logs/${SERVER}_${HST%%.foobar.com}_${PORT}"

 

>Logs are of following format: >>>>> | dtm=2012-05-03 15:00:00.295 PDT |

 

You could use sed to insert the escape sequences:

done < ${HSTFILE} | sed -e 's/\(.*dtm=\)\('$(date +"%Y-%m-%d")'\)\(.*$\)/\1ESC-start\2ESC-stop\3/'

 

Where ESC-start & ESC-stop are the appropriate escape sequences for your color.  You may want to use tput(1) to generate them.

Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 3 of 7 (611 Views)

Re: Colored timestamp

Hi Dennis,

For the sed statement I am getting -

sed -e '\(.*dtm=\)\('$(date +"%Y-%m-%d")'\)\(.*$\)/\1`tput smso`\2`tput rmso`\3/'

 

sed: 1: "\(.*dtm=\)\(2012-06-18\ ...": unterminated regular expression

 

Thanks,

Allan.

Acclaimed Contributor
Dennis Handly
Posts: 25,281
Registered: ‎03-06-2006
Message 4 of 7 (597 Views)

Re: Colored timestamp

>For the sed statement I am getting

 

Oops, I forgot the "s" command.  See my post above.

Trying to use tput(1) directly doesn't work if it contains a "&", a sed special.

This is what I had to do:

# Need to use echo to add a newline since sed won't work on non-text files.  Then remove the newline.

# Need to quote each "&" since special to sed.

SMSO=$(echo $(tput smso) | sed -e 's/\&/\\\&/g' -e 's/\\n//')
RMSO=$(echo $(tput rmso) | sed -e 's/\&/\\\&/g' -e 's/\\n//')

# while loop here

... sed -e 's/\(.*dtm=\)\('"$(date +"%Y-%m-%d")"'\)\(.*$\)/\1'"$SMSO"'\2'"$RMSO"'\3/'


Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 5 of 7 (377 Views)

Re: Colored timestamp

Reopening the thread.

 

Thanks to Dennis, I do get the date in colored , was wondering if we can get ERROR (all caps) also colored if it occurs in the logs.

 

Sample output which has ERROR.

 

>>>>> | dtm=2012-08-19 09:27:28.344 PDT | level=ERROR

 

Thanks,

Allan.

 

 

Acclaimed Contributor
Dennis Handly
Posts: 25,281
Registered: ‎03-06-2006
Message 6 of 7 (371 Views)

Re: Colored timestamp

>if we can get ERROR (all caps) also colored if it occurs in the logs.

 

If you don't care which date has the "ERROR" string:

sed -e ... -e "s/ERROR/${SMSO}ERROR${RMSO}/g"

Frequent Advisor
allanm77
Posts: 88
Registered: ‎06-27-2011
Message 7 of 7 (365 Views)

Re: Colored timestamp

thanks , that did it!
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.