Standard service - a fixed amount of work that is performed at a fixed price.  Post Service

  

Tuesday, 21 May 2019 02:04

Find files with errors

Written by  http://blog.mclaughlinsoftware.com/2019/05/21/find-files-with-errors/
Rate this item
(0 votes)

My students wanted a quick solution on how to find the log files that contain errors. That’s a simple line of code in Linux if you want any Oracle errors that

start with ORA-:

find$HOME/lab2 -type f |xargsgrep-i ora\-

find $HOME/lab2 -type f | xargs grep -i ora\-

It takes only a moment more to look for errors starting with ORA- or PLS-, like:

find$HOME/lab2 -type f |xargsgrep-i-e ora\- -e pls\-

find $HOME/lab2 -type f | xargs grep -i -e ora\- -e pls\-

The latter might return something like this:

contact_lab.txt:ORA-00904: "MEMBER_LAB_ID": invalid identifier contact_lab.txt:ORA-00942: table or view does not exist contact_lab.txt:ORA-00942: table or view does not exist member_lab.txt:ORA-02264: name already used by an existing constraint member_lab.txt:ORA-00955: name is already used by an existing object

contact_lab.txt:ORA-00904: "MEMBER_LAB_ID": invalid identifier contact_lab.txt:ORA-00942: table or view does not exist contact_lab.txt:ORA-00942: table or view does not exist member_lab.txt:ORA-02264: name already used by an existing constraint member_lab.txt:ORA-00955: name is already used by an existing object

You can improve the error identification by identifying line numbers by adding -n option, like:

find$HOME/lab2 -type f |xargsgrep-in-e ora\- -e pls\-

find $HOME/lab2 -type f | xargs grep -in -e ora\- -e pls\-

The latter might return something like this when there are two or more files:

contact_lab.txt:76:ORA-00904: "MEMBER_LAB_ID": invalid identifier contact_lab.txt:150:ORA-00942: table or view does not exist contact_lab.txt:157:ORA-00942: table or view does not exist member_lab.txt:75:ORA-02264: name already used by an existing constraint member_lab.txt:149:ORA-00955: name is already used by an existing object

contact_lab.txt:76:ORA-00904: "MEMBER_LAB_ID": invalid identifier contact_lab.txt:150:ORA-00942: table or view does not exist contact_lab.txt:157:ORA-00942: table or view does not exist member_lab.txt:75:ORA-02264: name already used by an existing constraint member_lab.txt:149:ORA-00955: name is already used by an existing object

Unfortunately, the command raises an error when there aren’t any files found of with a qualified extension. It also fails to prepend the file name when there’s only one qualified file name. As a result of these deficiencies, I’ve written the following Bash shell script. I’ve opted to call it the .findErrors.bashrc file name and deploy it in the user’s $HOME directory.

#!/bin/bash# Assign any file filter to the ext variable.ext=${1}# Assign the extension or simply use a wildcard for all files.if[!-z${ext}]; thenext="*.${ext}"elseext="*"fi# Assign the number of qualifying files to a variable.fileNum=$(ls-l${ext}2>/dev/null |grep-v ^l |wc -l)# Evaluate the number of qualifying files and process.if[${fileNum}-eq"0"]; thenecho"[0] files exist."elif[${fileNum}-eq"1"]; thenfileName=$(ls${ext})find`pwd`-type f |grep-in${ext}-e ora\- -e pls\- |whileIFS='\n'read list; doecho"${fileName}:${list}"doneelsefind`pwd`-type f |grep-in${ext}-e ora\- -e pls\- |whileIFS='\n'read list; doecho"${list}"donefi

#!/bin/bash # Assign any file filter to the ext variable. ext=${1} # Assign the extension or simply use a wildcard for all files. if [ ! -z ${ext} ]; then ext="*.${ext}" else ext="*" fi # Assign the number of qualifying files to a variable. fileNum=$(ls -l ${ext} 2>/dev/null | grep -v ^l | wc -l) # Evaluate the number of qualifying files and process. if [ ${fileNum} -eq "0" ]; then echo "[0] files exist." elif [ ${fileNum} -eq "1" ]; then fileName=$(ls ${ext}) find `pwd` -type f | grep -in ${ext} -e ora\- -e pls\- | while IFS='\n' read list; do echo "${fileName}:${list}" done else find `pwd` -type f | grep -in ${ext} -e ora\- -e pls\- | while IFS='\n' read list; do echo "${list}" done fi

You can check back for an updated script file because I plan to add recursion and regular expression search criteria. Unfortunately, I only had an hour or so to give this tonight. I plan to add the following function the student’s image in their .bashrc file:

# Calls the finderrors utility.finderrors(){eval"`$HOME/.findErrors.bashrc ${1}`"}

# Calls the finderrors utility. finderrors() { eval "`$HOME/.findErrors.bashrc ${1}`" }

Then, the students only need to call the finderrors function with or without a file extension to identify errors beginning with ORA- or PLS- in their log files. As always, I hope this helps those looking for a solution.

Read 51 times

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.