extract lines from text after keyword


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract lines from text after keyword
# 1  
Old 10-29-2012
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

Code:
hello
helloo
AAA
one
two
three
four
helloooo
hellooo

I want the output to be
Code:
one
two 
three 
four


Last edited by Scrutinizer; 10-29-2012 at 01:08 PM.. Reason: code tags
# 2  
Old 10-29-2012
if your grep supports -A option, then use it like

Code:
grep -A 4 "AAA" file.txt

else, you can try with awk

Code:
awk '/AAA/{for(i=1;i<=4;i++){getline;print}exit;}' file.txt

# 3  
Old 10-29-2012
with this code I get the line with the keyword too! I dont want this line!
# 4  
Old 10-29-2012
Try:
Code:
awk 'p-->0; /AAA/{p=c}' c=4 infile

# 5  
Old 10-29-2012
thank you! it works great!
if it's not trouble do you know how can I do this with grep?
# 6  
Old 10-29-2012
AFAIK this cannot be done with grep alone...
# 7  
Old 10-29-2012
ok! thnx again for the info
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to extract value after keyword in html

Using awk to extract value after a keyword in an html, and store in ts. The awk does execute but ts is empty. I use the tag as a delimiter and the keyword as a pattern, but there probably is a better way. Thank you :). file <html><head><title>xxxxxx xxxxx</title><style type="text/css"> ... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Need to extract the word after a particular keyword throughout the file..

Hi Everyone, Need help in extracting the hostname from the below output. Expected output: DS-TESTB-GDS-1.TEST.ABC.COM DS-TESTB-GDS-2.TEST.ABC.COM .... ... /tmp $ cat -n /tmp/patchreport 1 /usr/bin/perl /admin/bin/patch/applyPatches.pl --apply_patches... (4 Replies)
Discussion started by: thiyagoo
4 Replies

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

4. Shell Programming and Scripting

Extract Lines Containg a Keyword

Hi , I have two files, say KEY_FILE and the MAIN_FILE. I am trying to read the KEY_FILE which has only one column and look for this column data in the MAIN_FILE to extract all the rows that have this key. I have written a script to do so, but somehow it is not returning all the rows ( It... (4 Replies)
Discussion started by: Sheel
4 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

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: $ 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 Mon Apr 12... (6 Replies)
Discussion started by: geetap
6 Replies

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

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

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

10. 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
Login or Register to Ask a Question