English 中文(简体)
检查记录和打印记录中发现的错误
原标题:Shell script to check a log file and print the errors that are found in it
  • 时间:2015-06-09 07:02:32
  •  标签:
  • oracle
  • shell

我的描述很新。 我正试图写一个文字,以检查错误的日志(手法硬编码),我必须印刷含有错误的文字。 i 能够书写逻辑,但需要点人阅读用户投入文件。

Appreciate the help thanks.


Logic:

  1. Accept the logfile patch from user
  2. Check if the logfile is present or not
  3. If present search the file for lines containing the error string (eg. Error, ORA)
  4. Print the lines containing error strings , also write the output to a logfile

阅读用户的日志

3. 错误指示

search="ERROR"

set a path for output file

outfile="file1.txt"

Execution logic

find "$mydir" -type f -name "$filename" |while read file
  do
    RESULT=$(grep "$search" "$file")
      if [[ ! -z $RESULT ]]
         then
            echo "Error(s) in $file: $RESULT" >> "$outfile"
     fi
  done
问题回答

我不肯定你所说的“用点人读用用户投入的文件”。 我假定“点人”是文字论据。

您可以使用这一文字:

#!/bin/bash
expected=outfile
for f in $@
do
  if [ "$expected" = outfile ]; then
      OUTFILE=$1
      expected=search
  elif [ "$expected" = search ]; then
      SEARCH=$2
      expected=files
  elif [[ -f $f ]]; then
      RESULT=`grep "$SEARCH" $f`
      if [ -n "$RESULT" ]; then
           echo -e "
"
           echo "Error(s) in "$f":"
           echo $RESULT
           echo -e "
" >> $OUTFILE
           echo "Error(s) in "$f":" >> $OUTFILE
           echo $RESULT >> $OUTFILE
      fi
  fi
done

Invoke with:

scriptname outfile search files

:

  • scriptname: is the name of file containing the script.
  • outfile: the name of the output file
  • search: the text to be searched
  • files: one or many file name or file patterns.

例子(我假定文字名称是搜索器,正处在系统轨道):

searcherror errorsfound.txt primary /var/log/*.log

searcherror moreerrors.txt "ORA-06502" file1.log /var/log/*.log ./mylogs/*




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

热门标签