issue with grep -B option


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting issue with grep -B option
# 1  
Old 06-15-2012
issue with grep -B option

hi friends,
i have a file where every word is present in a new line for example:
more file1:
Quote:
delete
from
caste
as
a
,
caste1
as
b
where
a.col
=
b.col
i want to fetch previous line wherever i am getting "as" as a keyword.
i tried at home the follwing code in linex:
Code:
grep -B 1 "as" file1

ouput:
caste
caste1
it was working!!
but now i am working on ksh88. its not working . i am getting error as :
grep: illegal option -- B
Usage: grep -hblcnsviw pattern file . . .

that mean this option is not there in ksh88. is there any replacement for this command.

thanks in advance Smilie
# 2  
Old 06-15-2012
Awk

Hi,

Try this one,
Code:
awk '/as/{print p;}{p=$0;}' file

use nawk instead of awk if you dont have awk.
Cheers,
Ranga:-)

Last edited by rangarasan; 06-15-2012 at 03:20 AM..
This User Gave Thanks to rangarasan For This Post:
# 3  
Old 06-15-2012
Just tweaking the previous solution a bit to get the correct results:

Code:
awk '/^as$/{print p}{p=$0}' file

This User Gave Thanks to elixir_sinari For This Post:
# 4  
Old 06-15-2012
its working!!. could you please explin the "p=$0" part. thanks
# 5  
Old 06-15-2012
awk

we are storing the previous value in p variable and using whenever required.
This User Gave Thanks to rangarasan For This Post:
# 6  
Old 06-15-2012
thanks. one issue insted of hardcoding it, if i am storing the "as" keywornd in a file and serching it.
for example:
word="as"

Code:
awk '/^\$word$/{print p}{p=$0}' file

not getting anything Smilie . please help
# 7  
Old 06-15-2012
I assume that you meant "variable" instead of "file".

In that case, try

Code:
word="as"
awk "/^$word$/{print p}{p=\$0}" file

This User Gave Thanks to elixir_sinari For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

DB2 - Performance Issue using MERGE option

Dear Team, I am using DB2 v10.5 and trying to load huge data using MERGE option ( 10-12 Million) using below query. Basically it loads data from staging to target . Staging table schemaname.Customer_Staging has 12 Million records . Runs for 25 Mins for just select query. But while doing merge... (2 Replies)
Discussion started by: Perlbaby
2 Replies

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

3. UNIX for Dummies Questions & Answers

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... (6 Replies)
Discussion started by: bjdamon
6 Replies

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

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

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