Extract two lines before and after the 'search text'


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract two lines before and after the 'search text'
# 1  
Old 04-12-2010
Extract two lines before and after the 'search text'

Hi Guys,
I have a situation wherein I need to extract two lines from below the search string.

Eg.

Current:

Code:
 
$ grep "$(date +'%a %b %e')" alert.log
Mon Apr 12 03:58:10 2010
Mon Apr 12 12:51:48 2010
$

Here I would like the display to be something like

Code:
 
Mon Apr 12 12:51:48 2010
Thread 1 advanced to log sequence 1629 (LGWR switch)
Current log# 3 seq# 1629 mem# 0: /dborafiles/ipvpn/vpn/oraredo01/redo03.log

Please assist. Thank you.
# 2  
Old 04-12-2010
which OS?

try,
Code:
grep -A2 "$(date +'%a %b %e')" alert.log

for non GNU systems, an awk workaround,

Code:
awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=0 a=2 s="$(date +'%a %b %e')" alert.log

# 3  
Old 04-12-2010
Thanks Anchal, OS is SunOS.

---------- Post updated at 02:48 PM ---------- Previous update was at 02:45 PM ----------

Anchal,

the grep -A2 does not work on Sun OS.


about awk option, it gives me an error, are you able to help resolve this?

Code:
$ awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=0 a=2 s="$(date +'%a %b %e')" /dborafiles/ipvpn/vpn/admin/vpn/bdump/alert_vpn.log
awk: syntax error near line 1
awk: bailing out near line 1
$

# 4  
Old 04-12-2010
Try this:
Code:
awk '$0 ~ d {c=3} c-- > 0' d="$(date +'%a %b %e')" /dborafiles/ipvpn/vpn/admin/vpn/bdump/alert.log

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 5  
Old 04-12-2010
Thank you Franklin, it worked ok for two lines below the search string...
For my information ... if I need two lines above the search string ...what would be the code?
# 6  
Old 04-12-2010
One way:
Code:
awk '$0 ~ d {print b;print a; print;exit}{b=a;a=$0}' d="$(date +'%a %b %e')" /dborafiles/ipvpn/vpn/admin/vpn/bdump/alert.log

# 7  
Old 04-12-2010
What about this:

Code:
awk '{line[NR]=$0; if ($0 ~ /d/) ind=NR}END{for(i=ind-2;i<=ind+2;i++)if(i != ind)print line[i]}' d="$(date +'%a %b %e')" file3


Last edited by devtakh; 04-12-2010 at 07:44 AM.. Reason: changed no of lines
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to search a text in file and retrieve required lines following it with UNIX command?

I have requirement to search for a text in the file and retrieve required lines that is user defined with unix command. Eg: Find the text UNIX in the below file and need to return Test 8 & Test 9 Test 1 Test 2 Test 3 Test 4 UNIX Test 5 Test 6 Test 7 Test 8 Test 9 Result can... (8 Replies)
Discussion started by: Arunkumarsak4
8 Replies

2. Shell Programming and Scripting

Extract lines from text files

I have some files containing the following data # RESIDUE AA STRUCTURE BP1 BP2 ACC N-H-->O O-->H-N N-H-->O O-->H-N TCO KAPPA ALPHA PHI PSI X-CA Y-CA Z-CA 1 196 A M 0 0 230 0, 0.0 2,-0.2 0, 0.0 0, 0.0 0.000 360.0 360.0 360.0 76.4 21.7 -6.8 11.3 2 197 A D + 0 0 175 1,-0.1 2,-0.1 0, 0.0 0, 0.0... (10 Replies)
Discussion started by: edweena
10 Replies

3. Shell Programming and Scripting

extract lines from text after keyword

I have a text and I want to extract the 4 lines following a keyword! For example if I have this text and the keyword is AAA hello helloo AAA one two three four helloooo hellooo I want the output to be one two three four (7 Replies)
Discussion started by: stekanius
7 Replies

4. Shell Programming and Scripting

Search for a pattern,extract value(s) from next line, extract lines having those extracted value(s)

I have hundreds of files to process. In each file I need to look for a pattern then extract value(s) from next line and then search for value(s) selected from point (2) in the same file at a specific position. HEADER ELECTRON TRANSPORT 18-MAR-98 1A7V TITLE CYTOCHROME... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

5. UNIX for Dummies Questions & Answers

Help please, extract multiple lines from a text file

Hi all, I need to extract lines between the lines 'RD' and 'QA' from a text file (following). there are more that one of such pattern in the file and I need to extract all of them. however, the number of lines between them is varied in the file. Therefore, I can not just use 'grep -A' command.... (6 Replies)
Discussion started by: johnshembb
6 Replies

6. Shell Programming and Scripting

AWK: How to extract text lines between two strings

Hi. I have a text test1.txt file like:Receipt Line1 Line2 Line3 End Receipt Line4 Line5 Line6 Canceled Receipt Line7 Line8 Line9 End (9 Replies)
Discussion started by: TQ3
9 Replies

7. Shell Programming and Scripting

extract particular lines from text file

I have two files file A which have a number in every row and file B which contains few hundred thousand rows with about 300 characters in each row (csv) What I need is to extract whole rows from B file (only these which numbers are indicated in A file) I also need to use cygwin. Any... (7 Replies)
Discussion started by: gunio
7 Replies

8. Shell Programming and Scripting

Extract lines of text based on a specific keyword

I regularly extract lines of text from files based on the presence of a particular keyword; I place the extracted lines into another text file. This takes about 2 hours to complete using the "sort" command then Kate's find & highlight facility. I've been reading the forum & googling and can find... (4 Replies)
Discussion started by: DionDeVille
4 Replies

9. Shell Programming and Scripting

extract the lines between specific line number from a text file

Hi I want to extract certain text between two line numbers like 23234234324 and 54446655567567 How do I do this with a simple sed or awk command? Thank you. ---------- Post updated at 06:16 PM ---------- Previous update was at 05:55 PM ---------- found it: sed -n '#1,#2p'... (1 Reply)
Discussion started by: return_user
1 Replies

10. Shell Programming and Scripting

need to search text and output previous lines

I have a file (OMlog0) that is quite large, I need to find the line "Automatic Recharge Audit Process Finished" and output that line and the time stamp that occurs two lines previous. The line that I was using is "sed -n '/Automatic Recharge Audit Process Finished/,/No errors/p' /sn/log/OM* >... (8 Replies)
Discussion started by: grinds
8 Replies
Login or Register to Ask a Question