Unix shell script (grep -A 6 -B 2 "ORA-" filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix shell script (grep -A 6 -B 2 "ORA-" filename
# 1  
Old 04-24-2007
Unix shell script (grep -A 6 -B 2 "ORA-" filename

BACKGROUND:

I am using Solaris 10. Some of my boxes have gnu grep and I can use -A and -B flags on those. However, the solaris flavor of grep won't use the flags -A or -B. And some of my boxes won't be getting gnu grep.

Should I try using perl, awk, or sed?

Actual PROBLEM: I am grepping the oracle_alert.log for "ORA-". This solution means that I have to go read the alert.log manually. I would like to be able to cut out the 2 lines preceding and 6 lines following each (distinct) "ORA-" error message.

Thanks for your help.

Wayne
# 2  
Old 04-24-2007
Ps

I would like to point out that while I have done some scripting in the past, I am not great at any of the programs mentioned - perl, awk, sed.

I would like to know which tool set to learn up on to do this the best (cross shell capability.)

Thanks.

Wayne
# 3  
Old 04-25-2007
I Want To Get Output Like This

I WANT TO GET OUTPUT LIKE THIS
*
**
***
****
IN SHELL SCRPTING
USING VI EDITOR

I AHVE TRIED THIS

for i in 1 2 3 4
do
for j in $i
do
echo "*"
done
done
# 4  
Old 04-25-2007
Quote:
Originally Posted by el_guero
BACKGROUND:

I am using Solaris 10. Some of my boxes have gnu grep and I can use -A and -B flags on those. However, the solaris flavor of grep won't use the flags -A or -B. And some of my boxes won't be getting gnu grep.

Should I try using perl, awk, or sed?

It can be done with sed, but awk is the logical tool to use for this. See the article at http://www.unixreview.com/documents/s=9455/ur0412b/ for such a script.

# 5  
Old 04-25-2007
Quote:
Originally Posted by rahulspatil_111
I WANT TO GET OUTPUT LIKE THIS

Please do not post a new subject in an old thread. [Perhaps the moderator can move this into a new thread]

Quote:
*
**
***
****
IN SHELL SCRPTING
USING VI EDITOR
[INDENT]
What does the editor have to do with it? You can write a script in any editor, or even directly on the command line.

[/INDENT
Quote:
I AHVE TRIED THIS

for i in 1 2 3 4
do
for j in $i
do
echo "*"
done
done

Why do you expect $i to be interpreted as more than one argument in the inner loop, but not in the outer one?

You can use two loops if you want:

Code:
for i in 1 2 3 4
do
           j=0
           while [ $j -lt $i ]
           do
                      printf "%s" "*"
                      j=$(( $j + 1 ))
          done
          echo
done

But you only need one loop:

Code:
star=*
for i in 1 2 3 4
do
  printf "%s\n" "$star"
  star=$star*
done

# 6  
Old 04-25-2007
el_guero,
See if this works for you:
Code:
typeset -i cntLines=0
typeset -i cntORA
Prev1=""
Prev2=""
FoundORA="N"
while read ILine
do
 cntORA=`echo $ILine | egrep -c 'ORA-'`
 if [ $cntORA -gt 0 ]; then
   echo $Prev2
   echo $Prev1
   FoundORA="Y"
 fi
 if [ "$FoundORA" = "Y" ]; then
   echo $ILine
   cntLines=$cntLines+1
   if [ $cntLines -gt 6 ]; then
     cntLines=0
     Prev1=""
     Prev2=""
     FoundORA="N"
   fi
 else
   Prev2=$Prev1
   Prev1=$ILine
 fi
done < input_file

# 7  
Old 04-25-2007
Thank You,

Wayne

Quote:
Originally Posted by cfajohnson

It can be done with sed, but awk is the logical tool to use for this. See the article at http://www.unixreview.com/documents/s=9455/ur0412b/ for such a script.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

3. UNIX for Dummies Questions & Answers

UNIX rm -rf error "rm: cannot remove `filename' : Device or resource busy"

Hi Everyone, I am trying to remove a directory: $ rm -rf directory_name/ rm: cannot remove `directory_name/filename': Device or resource busy What does this mean, and why can't I remove these files? I already tried moving into the directory, and removing the files individually, but I... (7 Replies)
Discussion started by: Anna_Z
7 Replies

4. UNIX for Dummies Questions & Answers

"tail -n 1 filename" error while "head -n 1 filename" is ok?

Hi all, I was wondering why tail -n 2 filename produce an error when I manage to do similar command on head -n 2 filename SunOS{type8code0}: tail -n 2 filename usage: tail ] tail ] (2 Replies)
Discussion started by: type8code0
2 Replies

5. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies

6. Shell Programming and Scripting

grep -A 1 "string" filename

Dear all, can anyone pls provide equivalent of below code for solaris system.? grep -A 1 "string" filename the above command is working for Linux system. but i need same command for Solaris system thanks (8 Replies)
Discussion started by: steve2216
8 Replies

7. UNIX for Dummies Questions & Answers

the meaning of "!:*" in "alias foo 'command\!:*' filename"

Hi: How can I remove my own post? Thanks. (2 Replies)
Discussion started by: phil518
2 Replies

8. AIX

"too big" and "not enough memory" errors in shell script

Hi, This is odd, however here goes. There are several shell scripts that run in our production environment AIX 595 LPAR m/c, which has sufficient memory 14GB (physical memory) and horsepower 5CPUs. However from time to time we get the following errors in these shell scripts. The time when these... (11 Replies)
Discussion started by: jerardfjay
11 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. UNIX for Dummies Questions & Answers

correct syntax for using "grep regex filename" ?

I'm trying to grep a long ls by looking at the beginning of each filename for example: Many files begin with yong_ho_free_2005... Many files begin with yong_ho_2005... I can't just use "grep yong_ho" otherwise It'll display both files. So I'm trying to use a regex but my syntax is wrong. ... (2 Replies)
Discussion started by: yongho
2 Replies
Login or Register to Ask a Question