Use the -B option with grep


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Use the -B option with grep
# 1  
Old 04-03-2013
Use the -B option with grep

I am trying to parse out a file using grep - I would like to use the -B option so that the grep command prints the previous 2 lines if/when my string is matched - listed below are two examples - one with the info I want printed to a new file and one with info I want ignored. Any help with the grep -B command structure would be helpful.

Code:
______________________________________________________________________________
CAUAJM_I_50322 Checking Syntax for Inserting/Updating job: q_pm_d_bifind_curr_n2
CAUAJM_E_50442 Invalid attribute value for <priority>: Unexpected end of character string: Pri X--If job fails due to unable to allocate bytes of shared memory",restart. If fails f
or different reason,e-mail Oncall Database Management Production Systems Support.Product Mastering bifind pull for n2
CAUAJM_E_50246 JIL syntax check FAILED

Code:
CAUAJM_I_50322 Checking Syntax for Inserting/Updating job: q_feeds_d_dbqc_release_inact
CAUAJM_I_50247 JIL syntax check PASSED

# 2  
Old 04-03-2013
So, when FAILED, you want to see that line and the two previous?

Usually, you only need to write one rule - what to include. And you do not need a line of what not to include as it is the opposite of the first rule.
# 3  
Old 04-03-2013
Yes - when FAILED - I want to print two previous lines - I was showing what I didn't want to see and an example - I tried using grep 'string' -B 2 FILE but that is not working
# 4  
Old 04-03-2013
In what way is that 'not working'? Does it not have that option, or does it accept it yet misbehave somehow?

For systems which don't have -B, you can write a small awk script:

Code:
$ cat context.awk

# Recall N lines ago up to 9 lines
function last(N)
{
        if(N>L) return("");
        return(LINE[(L-N)%10]);
}

{ LINE[(++L)%10]=$0 } # Remember line for later

$0 ~ REGEX  {   for(N=2; N>=0; N--) print last(N);      }

$ awk -f context.awk REGEX="FAILED" inputfile

Use nawk on solaris.
These 2 Users Gave Thanks to Corona688 For This Post:
# 5  
Old 04-03-2013
I just prints line with matching string - not previous 2 lines
# 6  
Old 04-03-2013
It works fine here:

Code:
$ awk -f warnlog.awk REGEX="FAILED" - <<EOF
CAUAJM_I_50322 Checking Syntax for Inserting/Updating job: q_pm_d_bifind_curr_n2
CAUAJM_E_50442 Invalid attribute value for <priority>: Unexpected end of character string: Pri X--If job fails due to unable to allocate bytes of shared memory",restart. If fails f
or different reason,e-mail Oncall Database Management Production Systems Support.Product Mastering bifind pull for n2
CAUAJM_E_50246 JIL syntax check FAILED
EOF

CAUAJM_E_50442 Invalid attribute value for <priority>: Unexpected end of character string: Pri X--If job fails due to unable to allocate bytes of shared memory",restart. If fails f
or different reason,e-mail Oncall Database Management Production Systems Support.Product Mastering bifind pull for n2
CAUAJM_E_50246 JIL syntax check FAILED

$

Show exactly what you're doing, word for word, letter for letter, keystroke for keystroke.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 04-03-2013
Ok - I have it working now - thanks so much for your help.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep -C option not available

grep -C option is not availbale and i am using korn shell. Even -C option in man grep is not showing.. Input file : test.txt 111 333 444 555 aaa bbbb Command : grep -C1 555 test.txt Required output: 444 555 (8 Replies)
Discussion started by: NareshN
8 Replies

2. Shell Programming and Scripting

-v and -f option for grep not working

In solaris, i m trying to find the files having a particulat extension and then from the list i want to exclude those files which is present in a file. But it seems the -f and -v option are not working find $source -type f -name $extn | /usr/xpg4/bin/grep -F -v -f $exclude | while read... (7 Replies)
Discussion started by: millan
7 Replies

3. Shell Programming and Scripting

grep with cut option??

This is my command-line code in my script, passwd=`grep $passwd $userfile | cut -f2 -d: login_users > retrieve` the cut -f2 -d: login_users > retrieve searches and prints the whole column two. what I need is one of the items in the column two only.. what option can I add to my cut... (2 Replies)
Discussion started by: jenimesh19
2 Replies

4. Solaris

Grep Option

Hi, I was wondering, if there is any option with Grep, so that i can have couple of lines appearing before and after the search line within a result ? Thanks in Advance. Rgds - Prince. (4 Replies)
Discussion started by: john_prince
4 Replies

5. Shell Programming and Scripting

Need best grep option or alternative

Hello, I am processing a text file which contains only words with few combination of characters (it is a dictionary file). example: havana have haven haven't havilland havoc Is there a way to exclude only 1 to 8 character long words which not include space or special characters : '-`~.. so... (5 Replies)
Discussion started by: alekkz
5 Replies

6. Solaris

Grep -A -B -C option

Hi, Does anyone know why -A/B/C is not working with grep on my solaris box? Thanks, Prince (5 Replies)
Discussion started by: john_prince
5 Replies

7. UNIX for Dummies Questions & Answers

grep -v '#' option

Hi, I've the following code in my program. grep 'FILE_PREFIX' /cblconfig |grep -v '#' > $fpfx-filename I'm not sure what second grep does. Can someone explain this command? Thanks (1 Reply)
Discussion started by: ani12345
1 Replies

8. Shell Programming and Scripting

grep -m option

I am new to unix environment, can somebody help me with the grep options like -m. (3 Replies)
Discussion started by: vikas_kesarwani
3 Replies

9. UNIX for Advanced & Expert Users

grep using -f option

Dear all I have a file with more than one patters to search.Such as pattern.txt. I have to grep these patterns into a data file such as data.txt.how to do this ,i tried /usr/xpg4/bin/grep -f <pattern_file> <data_file> Its not working. why or how to search pattern file ? (3 Replies)
Discussion started by: tkbharani
3 Replies

10. UNIX for Advanced & Expert Users

grep -- option?

Does anyone know what the double dashes in the following grep line mean? grep -c -- "->" The intent of the line is to have a long listing of a directory piped to it, and for it to count off the symbolic links based on the "->" characters. I'm not sure why the scripter decided to search for... (1 Reply)
Discussion started by: dmwatan
1 Replies
Login or Register to Ask a Question