Results 1 to 15 of 15
  1. #1
    Join Date
    Dec 2004
    Posts
    2

    Unanswered: 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.

  2. #2
    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.

  3. #3
    Join Date
    Jun 2004
    Posts
    20
    grep -A2 -B2 "ORA-" init.ora

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

    HTH
    bye

  4. #4
    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
    +++

  5. #5
    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> |
    +-----------------------+

  6. #6
    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.

  7. #7
    Join Date
    Jun 2008
    Posts
    5
    Hi ,

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

    -Smita

  8. #8
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    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

  9. #9
    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

  10. #10
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    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.
    Last edited by stolze; 06-08-08 at 05:15.
    Knut Stolze
    IBM DB2 Analytics Accelerator
    IBM Germany Research & Development

  11. #11
    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

  12. #12
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    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

  13. #13
    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

  14. #14
    Join Date
    Jan 2007
    Location
    Jena, Germany
    Posts
    2,721
    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

  15. #15
    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

Posting Permissions

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