Solved

I Unix bash shell script how to check for "ERROR" within a .LOG file?

Posted on 2012-03-22
6
1,373 Views
Last Modified: 2012-03-23
In the Unix bash shell script, what would be an example of searching within Unix directory for a file ("sasprog1.LOG") and then checking for an ERROR within "sasprog1.LOG" file and then opening another file ("file1.txt") and then writing some messages to "file1.txt" file?

Please note: "file1.txt" may or may not exist in the Unix directory. "sasprog1.LOG" does exist in the Unix directory.

Any examples or comments on this will be very appreciated!!
0
Comment
Question by:labradorchik
  • 3
  • 3
6 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37753332
if grep -q "ERROR" sasprog1.LOG ;
   then echo "Error found in sasprog1.LOG" > file1.txt
fi
0
 

Author Comment

by:labradorchik
ID: 37753354
Can this be done in any other way?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37753388
find /start/dir -type f -name "sasprog1.LOG" |while read file
  do
    RESULT=$(grep "ERROR" $file)
      if [[ ! -z $RESULT ]]
         then
            echo "Error(s) in $file: $RESULT" >> file1.txt
     fi
  done
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:labradorchik
ID: 37753848
woolmilkporc, thank you for your example!
Question:
Would it work with referencing specific directories like in this example?  

mydir=/start/dir

find $mydir -type f -name "sasprog1.LOG" |while read file
  do
    RESULT=$(grep "ERROR" $file)
      if [[ ! -z $RESULT ]]
         then
            echo "Error(s) in $file: $RESULT" >> file1.txt
     fi
  done

Open in new window

0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 37753902
Yes, of course.

You can also have the filenames as variables, as well as the search string.
mydir="/start/dir"
filename="sasprog1.LOG"
search="ERROR"
outfile="file1.txt"

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

Open in new window



Please don't be astonished about the many quotation marks - you only have to use them if you suspect embedded spaces or special characters being present in the respective strings.
0
 

Author Comment

by:labradorchik
ID: 37753968
This is great!!  Exactly what I was looking for! Thank you!!
Could you please also check out another question that I have at:  http://www.experts-exchange.com/OS/Unix/Q_27644555.html
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article describes some very basic things about SQL Server filegroups.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now