1748053 Members
4828 Online
108758 Solutions
New Discussion юеВ

Re: Colored timestamp

 
SOLVED
Go to solution
allanm77
Frequent Advisor

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.

6 REPLIES 6
Dennis Handly
Acclaimed Contributor

Re: Colored timestamp

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.

allanm77
Frequent Advisor

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.

Dennis Handly
Acclaimed Contributor
Solution

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


allanm77
Frequent Advisor

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.

 

 

Dennis Handly
Acclaimed Contributor

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"

allanm77
Frequent Advisor

Re: Colored timestamp

thanks , that did it!