find a word and print n lines before and after the match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find a word and print n lines before and after the match
# 1  
Old 12-09-2011
find a word and print n lines before and after the match

how to find a word and print n lines before and after the match until a blank line is encounterd
# 2  
Old 12-09-2011
Homework ?? What is your try ..
# 3  
Old 12-09-2011
i googled and got some info..but cant put all together to make it work.. grep -n will give the line number of the word i am searching for .. and i took that line number in awk '{$value+1}' but this didnt work..

then saw getline utility in awk but it prints only one line..
# 4  
Old 12-09-2011
If not GNU grep ..

Before 5 lines ..
Code:
sed -n '/pattern/q;p' infile | tail -5

After 5 lines ..
Code:
nawk 'c-->0;/pattern/{print;c=5}' infile


Last edited by jayan_jay; 12-09-2011 at 03:44 AM..
# 5  
Old 12-09-2011
Quote:
Originally Posted by chidori
how to find a word and print n lines before and after the match until a blank line is encounterd
Which OS?

try grep -A and grep -B if those are available for you.
# 6  
Old 12-09-2011
solaris 10

grep -A and -B are for gnu tool right?

what i exactly want is..
Code:
G
M

A
B
C
D

E
F

Assume the above is the file content. i want to find for the letter 'B' and print lines above
and below it until a blank line is encountered.

so my result should be
Code:
A
B
C
D

# 7  
Old 12-09-2011
on solaris, grep -A and grep -B should work.

try
Code:
/usr/xpg4/bin/grep

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search for word in huge logfile and need to continue to print few lines from that line til find date

Guys i need an idea for one logic..in shell scripting am struggling with a logic...So the thing is... i need to search for a word in a huge log file and i need to continue to print few more lines from that line and the consecutive line has to end when it finds the line with date..because i know... (1 Reply)
Discussion started by: Prathi
1 Replies

2. Shell Programming and Scripting

Match the value & print lines from the match

Hello, I have a file contains two columns. I need to print the lines after “xxx” so i'm trying to match "xxx" & cut the lines after that. I'm trying with the grep & cut command, if there any simple way to extract this please help me. Sample file : name id AAA 123 AAB 124 AAC 125... (4 Replies)
Discussion started by: Shenbaga.d
4 Replies

3. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

4. Shell Programming and Scripting

How to match the first word and print only that line in UNIX?

Below is the file DISK-A 109063.2 49 31 40.79 DISK-B 110058.5 49 44 57.07 DISK-c 4402.4 2 1 2.14 from the file, i want to search for 'DISK-A' and print only that line with the first word matching to DISK-A and the output should skip DISK-A. Desired Output: (If i'm... (2 Replies)
Discussion started by: web2moha
2 Replies

5. Shell Programming and Scripting

Print string after the word match

Hi, I have the logs : cat logsx.txt 744906,{"reportingGroups":,"version":"2.0"} 678874,{"reportingGroups":,"version":"2.0"} 193571,{"reportingGroups":,"version":"2.0"} 811537,{"reportingGroups":,"version":"2.0"} 772024,{"reportingGroups":,"version":"2.0"}... (5 Replies)
Discussion started by: justbow
5 Replies

6. Shell Programming and Scripting

Print a word after a match

Hi all, I have the below line: 08 03 * * 1-5 XXXXXXXXXXXXX -ENVI LDNFOUAT10 -EXE xxxxxxxx -CONFIG \${xxxxx} -SUBCLASS RESET -START -EXTRAAPPARGS \" -env 38LDNFOUAT10 \" >> /tmp/SRRC_xxxxxxx_start.log.`/usr/bin/date +\%Y\%m\%d` 2>&1 I want to print just one word after the string "-env", in... (7 Replies)
Discussion started by: Cvg
7 Replies

7. Shell Programming and Scripting

Based on column in file1, find match in file2 and print matching lines

file1: file2: I need to find matches for any lines in file1 that appear in file2. Desired output is '>' plus the file1 term, followed by the line after the match in file2 (so the title is a little misleading): This is honestly beyond what I can do without spending the whole night on it, so I'm... (2 Replies)
Discussion started by: pathunkathunk
2 Replies

8. Shell Programming and Scripting

perl lwp find word and print next word :)

hi all, I'm new there, I'm just playing with perl and lwp and I just successfully created a script for log in to a web site with post. I have a response but I would like to have something like this: I have in my response lines like: <div class="sender">mimi020</div> <some html code.....>... (3 Replies)
Discussion started by: vogueestylee
3 Replies

9. Shell Programming and Scripting

print word after pattern match in two instances

i have a file like below. how can i printout the digits followed by the pattern -bwout and -bwin. say i run the script by entering line number 145 (the fourth line), then the o/p should be like 5000000 1024000 8 test1 -ipprot erp -ppsout 500 -ppsin 500 -bwout 300000 -bwin 300000 -statsdevice... (7 Replies)
Discussion started by: sb245
7 Replies

10. Shell Programming and Scripting

Print all the words after a match word

Hi, I want to print all words till the last word after the match of "ERROR" word. For e.g. I'll get an sqlplus error with e.g. 1 $ ./calltest_fn.ksh var test_var:=test_fn1; calltest_fn.ksh file1 file2 file3 ERROR at line 4: ORA-06550: line 4, column 11: PLS-00201: identifier... (5 Replies)
Discussion started by: dips_ag
5 Replies
Login or Register to Ask a Question