Note that there are some explanatory texts on larger screens.

plurals
  1. POShell script to validate logger date format in log file
    text
    copied!<p>I need to validate my log files:</p> <p>-All new log lines shall start with date.</p> <p>-This date will respect the ISO 8601 standard. Example: 2011-02-03 12:51:45,220Z - </p> <p>Using shell script, I can validate it looping on each line and verifying the date pattern. The code is below:</p> <pre><code>#!/bin/bash processLine(){ # get all args line="$@" result=`echo $line | egrep "[0-9]{4}-[0-9]{2}-[0-9]{2} [012][0-9]:[0-9]{2}:[0-9]{2},[0-9]{3}Z" -a -c` if [ "$result" == "0" ]; then echo "The log is not with correct date format: " echo $line exit 1 fi } # Make sure we get file name as command line argument if [ "$1" == "" ]; then echo "You must enter a logfile" exit 0 else file="$1" # make sure file exist and readable if [ ! -f $file ]; then echo "$file : does not exists" exit 1 elif [ ! -r $file ]; then echo "$file: can not read" exit 2 fi fi # Set loop separator to end of line BAKIFS=$IFS IFS=$(echo -en "\n\b") exec 3&lt;&amp;0 exec 0&lt;"$file" while read -r line do # use $line variable to process line in processLine() function processLine $line done exec 0&lt;&amp;3 # restore $IFS which was used to determine what the field separators are IFS=$BAKIFS echo SUCCESS </code></pre> <p>But, there is a problem. Some logs contains stacktraces or something that uses more than one line, in other words, stacktrace is an example, <strong>it can be anything</strong>. Stacktrace example:</p> <pre><code>2011-02-03 12:51:45,220Z [ERROR] - File not found java.io.FileNotFoundException: fred.txt at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java) at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java) at ExTest.readMyFile(ExTest.java:19) at ExTest.main(ExTest.java:7) ... </code></pre> <p>will not pass with my script, but is valid! Then, if I run my script passing a log file with stacktraces for example, my script will failed, because it loops line by line. </p> <p>I have the correct pattern and I need to validade the logger date format, but I don't have wrong date format pattern to skip lines.</p> <p>I don't know how I can solve this problem. Does somebody can help me?</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload