LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-23-2007, 07:01 AM   #1
consty
Member
 
Registered: Feb 2004
Location: Douala-Cameroun
Distribution: RedHAt 9 ES
Posts: 85

Rep: Reputation: 15
analyse a shell script


Hi all,
I have a file with patterns like the one hereafter, patterns begin and end with ======, there are many lines inside a pattern. It's a log and what I want to do is to check every pattern to look if the string ERROR exists and if so print that string and the date, hour and Reference contained in the pattern.
I need help to write the script shell.
Thanks in advance
Consty

======
05/02/2007 04:51:02 [8714]: abdndala.c01.07: SAMS can not be sent, TYPE=DOUBE, STATE: 342184546 (slipt 6,Entity:ANY).
ERROR: 4685 Reference :=172910706273
absolute MM :=)(A1225:clAd=0000)(29746:messi :=624020100762629)(2747:hel_lang=3)(K2749:
sams_id=398)
======
 
Old 02-23-2007, 07:41 AM   #2
waelaltaqi
Member
 
Registered: Sep 2005
Location: USA, TN
Distribution: CentOS & Ubuntu for Desktop
Posts: 454

Rep: Reputation: 31
try to read some information about sed and grep commands. they are great text filtering and searching tools. google and sed and grep and you'll get the results you need. post the log file if you can.
the best i can think off for now if trying the following:
Code:
cat error file | grep ERROR*
 
Old 02-23-2007, 08:02 AM   #3
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
The problem with grep is that it's exclusively line-based. He needs to know the date and time, but these are contained on a line that comes before the one that tells him if he needs to keep that information at all. This is a situation where Perl comes in really handy. Not knowing the fully syntax of the log file, here's a simple script I came up with:
Code:
#!/usr/bin/env perl

use warnings;
use strict;

my $line = readline STDIN;

while (defined $line) {
    if ($line =~ m!======!) {
        my $date = '';
        my $time = '';
        my $error = 0;
        my $reference = '';
        $line = readline STDIN;
        while (defined $line && $line !~ m!======!) {
            if ($line =~ m!(\d\d/\d\d/\d\d\d\d) (\d\d:\d\d:\d\d)!) {
                $date = $1; $time = $2;
            }
            elsif ($line =~ m!^ERROR!) {
                $error = 1;
                $line =~ m!Reference :=(\d+)!;
                $reference = $1;
            }
            $line = readline STDIN;
        }
        if ($error) {
            print "ERROR: date = $date | time = $time | reference = $reference\n";
        }
    }
    else {
        $line = readline STDIN;
    }
}
This is the log file I used for testing:
Code:
======
05/02/2007 04:51:02 [8714]: abdndala.c01.07: SAMS can not be sent, TYPE=DOUBE, STATE: 342184546 (slipt 6,Entity:ANY).
ERROR: 4685 Reference :=172910706273
absolute MM :=)(A1225:clAd=0000)(29746:messi :=624020100762629)(2747:hel_lang=3)(K2749:
sams_id=398)
======
05/03/2007 04:51:02 [8714]: blahblah.c01.07: SAMS can not be sent, TYPE=DOUBE, STATE: 342184546 (slipt 6,Entity:ANY).
ERROR: 4685 Reference :=12345678
absolute MM :=)(A1225:clAd=0000)(29746:messi :=624020100762629)(2747:hel_lang=3)(K2749:
sams_id=398)
======
05/03/2007 04:51:02 [8714]: foobar.c01.07: SAMS sent successfully, TYPE=DOUBE, STATE: 342184546 (slipt 6,Entity:ANY).
absolute MM :=)(A1225:clAd=0000)(29746:messi :=624020100762629)(2747:hel_lang=3)(K2749:
sams_id=398)
======
Where the first two contain errors and the last one doesn't. Even if that's not quite correct, the script may still work right.
 
Old 02-23-2007, 08:21 AM   #4
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 38
This might get you started.
Print paragraphs that contain ERROR:
Code:
sed -e '/./{H;$!d;}' -e 'x;/ERROR/!d;' my_file
Then drill down, maybe using more sed commands to get the data and blah.
Some people might tell you to use perl.

If you need more info for sed: post more input and intended output.
Good link for sed:
http://www.linuxquestions.org/bookmarks/tags/sed
 
Old 02-23-2007, 11:26 AM   #5
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
Quote:
Originally Posted by taylor_venable
The problem with grep is that it's exclusively line-based.
Actually, grep can be used in this case as well using the -B option to print out lines that occur before the match occurs (-A if you want lines after you match). Ex:

Code:
#!/bin/bash
# Create the error listing.  By default, grep 
#  will separate the lines with a '--' string
error_string=$(grep -B 1 "ERROR" log.file)

# Loop through the error_string variable until
#  there is no more information in it.
while [ ${#error_string} -gt 0 ]
do
 # Pull out the first error string
 error_string2=${error_string%%--*}

 # Remove the first error string, enclose in double
 #  quotes to handle slashes
 error_string=${error_string//"${error_string2}"/}

 # Remove the -- from the string.  Can not be done
 #  on the previous line as there is no -- at the 
 #  end of the string, and you end up in an 
 #  infinite loop
 error_string=${error_string/--/}

 # echo out the date, time, error, and reference
 echo $(echo ${error_string2} | cut -d' ' -f1,2) ERROR$(echo ${error_string2##*ERROR})

done
This is obviously not the best way to parse the error log (a big log would totally crush this code to the point of it being unusable, trust me), and would suggest using one pearl or sed as mentioned, but wanted to point out that grep can be used for more than displaying just the line that matches your text.

Last edited by Hobbletoe; 02-23-2007 at 11:29 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
Call One shell script from another shell script Sundaram Linux - Software 5 10-13-2006 03:59 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 06:29 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration