Grep'ing information from a log file on SUN OS 5


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep'ing information from a log file on SUN OS 5
# 1  
Old 11-18-2011
Grep'ing information from a log file on SUN OS 5

Hi Guys,
I'm trying to write an script that will be launched by a user. The script will look at a log file and check for alerts with the date (supplied by user) and a machine's hostname (also supplied by the user). I'm trying to get the output formatted just like the log file.
The logfile looks like this:
-----

popup on localmachine

Code:
Date/Time:      MM/DD/YYYY
Host:           hostname
Alert Type:     ALARM
Parameter:      Paramiter information

Alarm information goes here
-----

The script I'm working with looks like this:
Code:
#!/bin/bash
echo "Enter the machine name followed by [ENTER]:"
read HOST

echo "Enter the date you wish to check for MM/DD/YYYY":
read DATE

ssh hostname grep '$HOST|$DATE' /path/to/file.log
echo

I've tried several variations of this, but all I get is gobbledygookSmilie. Can someone point me in the right direction? Any help is appreciated

Last edited by pludi; 11-18-2011 at 04:33 AM..
# 2  
Old 11-18-2011
Try to replace the single quotes with double quotes and add another pair of escaped quotes for the shell on hostname, so that the pipe character is not interpreted by the shell there.
Code:
ssh hostname grep "\"$HOST|$DATE\"" /path/to/file.log

# 3  
Old 11-19-2011
Hi Hergp, I tried it, and it gave me no output at all. Even when I grep for an entry that I know is in the log.
It's so frustrating being owned by something that seems like it would be a piece of cake. grrrrrrrr Smilie
# 4  
Old 11-20-2011
Is the string that you are searching for on the remote host literallyhostname|date, or are you wanting to grep for records with either host OR date in them?

If you want to grep for host OR date you need to use egrep or supply the -E option:

Code:
ssh hostname "egrep '$DATE|$HOST' "

# 5  
Old 11-20-2011
Hi Agama,
The information appears in the log in this format:
Date/Time: MM/DD/YYYY
Host: hostname
Alert Type: ALARM
Parameter: Paramiter information
Entries are seperated by a "-----------------"
What I'm trying to accomplish, is to make a simple check for end users to use so they can see if there were any alerts on a given day.
Any help is much appreciated
# 6  
Old 11-20-2011
You can try adding this to your script. Be careful with the quotes on the first and last lines. The awk programme needs to be quoted in single quotes on the remote machine, and you don't want the local shell to substitute in for $x references, so the quoting is tricky.

Code:
ssh hostname awk -v targeth=$HOST -v targetd=$DATE "'"'
    function print_stuff( atend )
    {
        if( date )
        {
            pcount++;
            printf( "%s\n%s\n%s\n%s\n\n", date, host, alert, parameter );
        }
        else
            if( atend  && ! pcount )
                printf( "no alarms found for %s\n", targeth );
    }

    /^Date.Time: / {
        print_stuff( 0 );   # print last block if collected
        date = targetd == $2 ? $0 : "";
        next;
    }

    /^Host: / && date {
        host = $0;
        if( $2 != targeth )
            date = "";      # reject; wrong date
        next;
    }

    /^Alert Type: / {
        alert = $0;
        next;
    }

    /^Parameter: / {
        parameter = $0;
        next;
    }

    END {
        print_stuff( 1 );               # print last block if it was collected
    }
'"' <path-to-logfile"

Hope this gets you closer.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed / awk / grep to extract information from log

Hi all, I have a query that runs that outputs data in the following format - 01/09/12 11:43:40,ADMIN,4,77,Application Group Load: Name(TESTED) LoadId(5137-1-0-1XX-15343-15343) File(/dir/dir/File.T03.CI2.RYR.2012009.11433350806.ARD) InputSize(5344) OutputSize(1359) Rows(2) Time(1.9960)... (8 Replies)
Discussion started by: jeffs42885
8 Replies

2. UNIX for Dummies Questions & Answers

Grep? - using a file of terms to search another file when the information is on a different line

I have a flat file that looks like this, let's call it Chromosome_9.txt: FT /Gene_Name="Guanyl-Acetylase 9" FT /Gene_Number"36952" FT /Gene_Name="Endoplasmic Luciferase" FT /Gene_Number"36953" FT ... (4 Replies)
Discussion started by: Twinklefingers
4 Replies

3. UNIX for Dummies Questions & Answers

grep'ing a variable - why not working

Hi all, Am writing a ksh script where I am looking for processes that has gone defunct and all of which has the same PPID PID is the variable that I need to match as this is the process ID of the processes that has gone defunct Am just curious how come the following DOES NOT work? ps... (6 Replies)
Discussion started by: newbie_01
6 Replies

4. Shell Programming and Scripting

grep'ing dot history file

Hi, I tried to grep ".sh_history" (DOTsh_history) file and did not return anything though I found the word in .sh _history file through vi editor in Linux. Then I tried to grep ".profile" to check if it is the prob with hidden files and I got results. Then I verified the same with my friend... (4 Replies)
Discussion started by: bobbygsk
4 Replies

5. Shell Programming and Scripting

copying file information using awk & grep

Hi, TASK 1: I have been using this code to print the information of files kept at "/castor/cern.ch/user/s/sudha/forPooja" in some text file name FILE.txt. rfdir /castor/cern.ch/user/s/sudha/forPooja | grep data | awk '{print "rfio:///castor/cern.ch/user/s/sudha/forPooja/"$9}' > FILE.txt ... (6 Replies)
Discussion started by: nrjrasaxena
6 Replies

6. Shell Programming and Scripting

grep'ing a variable that contains a metacharacter ($) with a while loop

This is driving me crazy, and I'm hoping someone can help me out with this. I'm trying to do a simple while loop to go through a log file. I'm pulling out all of the lines with a specific log line, getting an ID from that line, and once I have a list of IDs I want to loop back through the log and... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

7. Shell Programming and Scripting

pipe'ing grep output to awk

This script is supposed to find out if tomcat is running or not. #!/bin/sh if netstat -a | grep `grep ${1}: /tomcat/bases | awk -F: '{print $3}'` > /dev/null then echo Tomcat for $1 running else echo Tomcat for $1 NOT running fi the /tomcat/bases is a file that... (2 Replies)
Discussion started by: ziggy25
2 Replies

8. Shell Programming and Scripting

grep'ing a file until a certain message appears

Hello, I'm writing a script that will automate the launch of some services on my AIX machine. However, some services are dependent on the successful startup of others. When I start these services manually, I usually just check a log file until I see a message that confirms a successful... (3 Replies)
Discussion started by: pallak7
3 Replies

9. Shell Programming and Scripting

grep'ing and sed'ing chunks in bash... need help on speeding up a log parser.

I have a file that is 20 - 80+ MB in size that is a certain type of log file. It logs one of our processes and this process is multi-threaded. Therefore the log file is kind of a mess. Here's an example: The logfile looks like: "DATE TIME - THREAD ID - Details", and a new file is created... (4 Replies)
Discussion started by: elinenbe
4 Replies

10. UNIX for Dummies Questions & Answers

grep'ing for text within a bunch of files...?

I have, say, a dozen files, and I want to grep for a string of text within them. I don't remember the exact syntax, but let me give it a shot and show you an idea here... find . -type f -exec grep thisword {} \; ...and there's a way to put more than one grep into the statement, so it will tell... (1 Reply)
Discussion started by: kitykity
1 Replies
Login or Register to Ask a Question