?
Solved

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

Posted on 2012-03-22
6
Medium Priority
?
2,135 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

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 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

765 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