Solved

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

Posted on 2012-03-22
6
1,629 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
Creating and Managing Databases with phpMyAdmin in cPanel.
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.

789 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