If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > printing +2 and -2 line numbers including the grep word line.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-02-04, 13:57
graval graval is offline
Registered User
 
Join Date: Dec 2004
Posts: 2
printing +2 and -2 line numbers including the grep word line.

Hi Script Gurus,

I want to grep certain word from file and put it to some other file with proceeding 2 lines and next two lines of greped word line.

Here is the example of init.ora file below and I want to grep lines with which have word "ORA-" and I also want +2 and -2 line with the greped line.
---------- clip init.ora ----------

Tue Nov 30 00:25:35 2004
Thread 1 advanced to log sequence 59709
Tue Nov 30 00:25:35 2004
Current log# 9 seq# 59709 mem# 0: /d/db01/oradata/oss/redo09.dbf
Tue Nov 30 00:25:35 2004
ARC4: Beginning to archive log# 8 seq# 59708
Tue Nov 30 00:25:47 2004
ORA-000060: Deadlock detected. More info in file /d/db01/app/oracle/admin/oss/udump/ora_15029_oss.trc.
Tue Nov 30 00:26:39 2004
ARC4: Completed archiving log# 8 seq# [/B] 59708Tue Nov 30 00:26:57 2004
Thread 1 advanced to log sequence 59710
Current log# 6 seq# 59710 mem# 0: /d/db01/oradata/oss/redo06.dbf
Tue Nov 30 00:26:57 2004
ARC1: Beginning to archive log# 9 seq# 59709
Tue Nov 30 00:28:04 2004
--------- clip end --------

Any help on this?

Thanks in advance.

graval.
Reply With Quote
  #2 (permalink)  
Old 12-03-04, 17:23
jmdevil jmdevil is offline
Registered User
 
Join Date: Oct 2003
Posts: 9
I'd do the following: (yes, it is ugly)

awk "\$0 ~ /ORA-/ {
cmd=\"awk 'NR>=\" NR-2 \" && NR<=\" NR+2 \"' init.ora\"
system(cmd)
}" init.ora


Be very careful about the quoting.
Reply With Quote
  #3 (permalink)  
Old 12-06-04, 05:54
srsjc srsjc is offline
Registered User
 
Join Date: Jun 2004
Posts: 20
grep -A2 -B2 "ORA-" init.ora

-A --- After
-B --- Before

HTH
bye
Reply With Quote
  #4 (permalink)  
Old 12-06-04, 09:06
jmdevil jmdevil is offline
Registered User
 
Join Date: Oct 2003
Posts: 9
Unfortunately the -A and -B options are not available on every grep implementation.

Another trick to get it done is

ex -R init.ora << +++
g/ORA-/.-2,.+3p
q
+++
Reply With Quote
  #5 (permalink)  
Old 12-06-04, 09:29
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
Code:
sed -e '
   1{$!N;$d;}
   $!N;/ORA/!D
   $!N;$d;N;p
   g;$!N;$d;N;D
' init.ora
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #6 (permalink)  
Old 12-08-04, 15:22
graval graval is offline
Registered User
 
Join Date: Dec 2004
Posts: 2
Thanks it works...

Thank you guys. it work.

I am able to run awk and ex but i dont know sed one is not going thru.
ex is faster than awk so finally i decided to use ex.

Thank you all for your time and efforts.

Regards,

graval.
Reply With Quote
  #7 (permalink)  
Old 06-05-08, 00:26
smita_mishra2 smita_mishra2 is offline
Registered User
 
Join Date: Jun 2008
Posts: 5
Hi ,

I tried above unix commands but none of them worked..
Can some help me ??

-Smita
Reply With Quote
  #8 (permalink)  
Old 06-05-08, 11:53
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Could you describe your problem and your idea of "none of them worked" to us?
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #9 (permalink)  
Old 06-06-08, 12:15
smita_mishra2 smita_mishra2 is offline
Registered User
 
Join Date: Jun 2008
Posts: 5
Hi,

I want to perform the same action as the thread is about.
I have file and I want to get +2 and -2 lines from the line that has a specific word.

For example: I have log file in xml format and compressed with .gz and want to get the +2 and -2 lines in the file whereever it encounters "550" .

let me know if its not explaintory, I will try to expalin more..

Also I tried to run the commands below on .txt file but it didnt work.

ex -R init.ora << +++
g/ORA-/.-2,.+3p
q
+++


sed -e '
1{$!N;$d;}
$!N;/ORA/!D
$!N;$d;N;p
g;$!N;$d;N;D
' init.ora


awk "\$0 ~ /ORA-/ {
cmd=\"awk 'NR>=\" NR-2 \" && NR<=\" NR+2 \"' init.ora\"
system(cmd)
}" init.ora


Thanks,
Smita
Reply With Quote
  #10 (permalink)  
Old 06-08-08, 04:10
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
How about using "grep" with the -B or -C (?) options? The GNU grep utility allows you to specify exactly how many lines before or after a matching row should be written to the output as well.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development

Last edited by stolze; 06-08-08 at 04:15.
Reply With Quote
  #11 (permalink)  
Old 06-08-08, 23:48
smita_mishra2 smita_mishra2 is offline
Registered User
 
Join Date: Jun 2008
Posts: 5
i am getting error while trying grep -B or -c

$ grep -C 2 "ORA-" init.ora
grep: illegal option -- C
Usage: grep -hblcnsviw pattern file . . .
$ grep -B 2 "ORA-" init.ora
grep: illegal option -- B
Usage: grep -hblcnsviw pattern file . . .

-S
Reply With Quote
  #12 (permalink)  
Old 06-09-08, 02:37
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
I had a look at the man page of GNU grep again:
-A num - number of rows after the matching one
-B num - number of rows before the matching one
-C num - context

If this doesn't work for you, you probably don't use GNU grep.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #13 (permalink)  
Old 06-09-08, 10:33
smita_mishra2 smita_mishra2 is offline
Registered User
 
Join Date: Jun 2008
Posts: 5
how can I find if i am using GNU grep ?? Is there any way I can use GNU grep?

If I cant use grep to server my purpose, any other way of serving my request?

thanks a lot for ur help,
-s
Reply With Quote
  #14 (permalink)  
Old 06-09-08, 13:28
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You can install GNU grep, of course. Typically, the first thing you do with a non-GNU system is to install a whole bunch of GNU tools (like tar, grep, less, ...)

The alternative is that you write a small script in Perl, AWK or whatever programming language you prefer. Personally, I would prefer to take the right tool for the job if it is available and I wouldn't go through the hassle to write such a script myself.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #15 (permalink)  
Old 06-10-08, 16:24
smita_mishra2 smita_mishra2 is offline
Registered User
 
Join Date: Jun 2008
Posts: 5
unfortunately, I dont have authority to install anything on the server as its managed by other gp and they are not read yto do it..
do you any script that can serve the prupose?

thanks
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On