Solved

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

Posted on 2012-03-22
6
1,743 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

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.

Question has a verified solution.

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

Suggested Solutions

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.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

756 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