Solved

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

Posted on 2012-03-22
6
1,472 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

863 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

23 Experts available now in Live!

Get 1:1 Help Now