I need a script which converts log files into easily viewable .html files, accessible by "anyone".
Here is what I have so far:
#!/bin/bash
## The purpose of this script is to create .html files from log files, with the ability to optionally GREP for certain strings
if [ "$2" == "" ];
then
echo "Usage : $0 [Source Log file] [Output HTML file] [String to grep (if applicable)]"
exit 255
fi;
LOGFILE=$1
OUTPUTFILE=$2
GREPSTRING=$3
if [ "$3" == "" ];
then
echo "Cat'ing $LOGFILE to $OUTPUTFILE"
LOGCONTENTS=`cat $LOGFILE`
else
echo "Grep'ing for $GREPSTRING in $LOGFILE"
LOGCONTENTS=`grep --color $GREPSTRING $LOGFILE | sed -e "s/^.*$/&1\n/"`
fi;
# Below is the html heading which will be appended to the final .html file
HEADING="<html><body><h1>Log output for: $LOGFILE</h1>"
# Below is the end of the html file
END="</body></html>"
# Below all the prepared variables are stitched together to the OUTPUT/LOG FILE
echo $HEADING > $OUTPUTFILE
echo $LOGCONTENTS >> $OUTPUTFILE
echo $END >> $OUTPUTFILE
# For debugging, enable the lines below
echo $LOGCONTENTS
echo "Done preparing $OUTPUTFILE"
My problem is that the output, no matter how much I play with CAT, GREP, SED etc, does not preserve line breaks. It is essential for the output file to look more or less like when doing a normal tail -f or cat.
Instead of copying things into variables (and then misquoting them), here's a significant refactoring.
#!/bin/sh
exec >"$2"
cat <<HERE
<html><body><h1>Log output for: $1</h1>
<pre>
HERE
grep "${3:-^}" "$1"
echo '</pre></body></html>'
Because HTML cannot display terminal color codes anyway, I took out the --color option. If you have a suitable filter for translating to HTML color tags, by all means throw it in.
Notice also that the regex will default to one which matches all lines if you don't supply a third command-line argument.
Many browsers will render the lone opening <pre> tag as an unattractive empty line, but fixing that is hardly essential here. Break it out into a separate echo -n '<pre>' if it bothers you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With