Find the answer to your Linux question:
Results 1 to 4 of 4
Does anyone know how I can go about reading a file and for example seeing if it contains the phrase "ERROR"? and if it does contain it send an email ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    6

    Read file for phrases

    Does anyone know how I can go about reading a file and for example seeing if it contains the phrase "ERROR"? and if it does contain it send an email showing the line?

  2. #2
    Blackfooted Penguin daark.child's Avatar
    Join Date
    Apr 2006
    Location
    West Yorks
    Posts
    4,344
    Using what programming language? If shell programming some commands that come to mind are grep and mail.

  3. #3
    Just Joined!
    Join Date
    Mar 2008
    Posts
    6
    Quote Originally Posted by daark.child View Post
    Using what programming language? If shell programming some commands that come to mind are grep and mail.
    oh, sorry, shell scripting.

    Something as simple as this would work right?

    cat file.txt | grep "ERROR"

    Now how do I get it to send the name of the file and the line that contains "ERROR..." in an email?

  4. #4
    Just Joined!
    Join Date
    Mar 2008
    Posts
    6
    Ok, so I figured out how to write this.

    Code:
    #!/bin/sh
    
    FILE="file.txt"
    PHRASE="ERROR"
    
    message=$(cat $FILE | grep $PHRASE)
    
    echo "Message" | mail -s "Found $FILE containing $PHRASE" someone@someone.com
    I want to make this more efficient so I was thinking I could have one file that contains all the files I want to read along with the phrase I want to search for.
    So for example
    Code:
    mainfile.log
    
    file1.log    ERROR1
    file2.log    ERROR2
    file3.log    ERROR3
    So I want the code to read this file and then go through each file listed inside looking for the phrases.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...