read the first 10 lines below a keyword found by grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting read the first 10 lines below a keyword found by grep
# 1  
Old 06-23-2010
read the first 10 lines below a keyword found by grep

Hello Everyone,
i need to read specific number of lines ( always serialized ; i.e from 10 to 20 or from 34 to 44 ) in a file , where the first line is found by grep 'ing a keyword.
example

file.txt
------------------------------------------------------------------
--header
this is the message header 3x3
--body
RowId 12
TypeID 0xA
InfoA None
InfoB None
InfoC None
......
.....


----------------------------------------------------------------

so i want to read the first 10 lines begining from the line where 0xA is found.

how can this be achieved?

Appreciate Any help

Alain
# 2  
Old 06-23-2010
Try this...
Code:
nawk '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=2 a=4 s="string" file1

...where "b" and "a" are the number of lines to print before and after string "s".

In your case......

Code:
nawk '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=9 s="0xA" file.txt

# 3  
Old 06-23-2010
maybe this?:

Code:
awk 'BEGIN{printline=0;cont=1} {if($2=="0xA")printline==1;if(printline==1 && cont<=10){print $0;cont++}}' IN_FILE

# 4  
Old 06-23-2010
Many thanks Gentlmen.
will try and get back to you if needed.
# 5  
Old 06-23-2010
Code:
 
# grep -A10 "0xA" file

# 6  
Old 06-23-2010
Hi Again,

this is working fine
Code:
nawk '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=9 s="0xA" file.txt

can i make every 10 lines be on one line in the output separated by a delimiter perhaps ?

Alain

Last edited by Franklin52; 06-23-2010 at 08:30 AM.. Reason: Please use code tags
# 7  
Old 06-23-2010
You may use 'paste' if you require something like (Delimiter "|")

TypeID 0xA|InfoA None|InfoB None.........................

Code:
nawk '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=9 s="0xA" file.txt | paste -s -d"|" -

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep multi line with keyword ?

Hello Everyone, i need to grep specific keyword in a file. i want need solution to output. example file.txt 03-08-2019 21:02:20,938 ::: Recieve Data From Amazon ::: 03-08-2019 21:02:20,938 IP : 192.168.1.1 | msg = Your confirmation code for 'Verify phone number' is xxxxx | sno =... (2 Replies)
Discussion started by: ooilinlove
2 Replies

2. Shell Programming and Scripting

Issue in using read keyword twice

Hi, I have a situation where i need to read line by line from a text pad and with each line , i need to take inputs from command line and do some process. Using below code i am not able to use 'read' keyword twice. Can any one please help cat > t.txt a d c > cat > t.ksh while read... (4 Replies)
Discussion started by: Ravindra Swan
4 Replies

3. Shell Programming and Scripting

How to print few lines before and after matching word is found suing grep?

Hi, here are few lines present in the logs. I want to grep on Error and print few lines before and after Error word is found line1 Line2 Line3 Error Line4 Line5 Line6 Line7 I want the output to be Line2 Line3 Error Line5 (1 Reply)
Discussion started by: arghadeep adity
1 Replies

4. Shell Programming and Scripting

How to grep multi line with keyword ?

Hi All. how to grep with same keyword. 23-07-2012 15:15:30,117 ::: Recieve Message From Commadu ::: .. ... .... 23-07-2012 16:15:28,481 ::: Recieve Message From Commadu ::: 23-07-2012 16:15:28,481 IP : 127.0.0.1 | msg =... (1 Reply)
Discussion started by: ooilinlove
1 Replies

5. Shell Programming and Scripting

printing a text until a keyword is found

Hi, here's the problem: text="hello1 hello2 world earth mars jupiter planet"how do I print the text until it finds the keyword "mars" so that the desired output is output="hello1 hello2 world earth" I have rtfm of sed and I think the problem is, that if I find the word "mars" it will... (7 Replies)
Discussion started by: icantfindauser
7 Replies

6. Shell Programming and Scripting

Remove keyword found in title

Hello All, I have a bunch of files that have the following format, where the title is INPUT.txt and contains the following text: INPUT-FILLER1 204 INPUT-FILLER2 FILLER6-INPUT 5 FILLER-INPUT I want to go through the directory and remove the keyword INPUT. For example, my output would be... (5 Replies)
Discussion started by: jl487
5 Replies

7. Shell Programming and Scripting

multiple search keyword in grep

Dear All, I have a file containing info like TID:0903 asdfasldjflsdjf TID:0945 hjhjhkhkhkh TID:2045 hjhjhkhkhkh TID:1945 hjhjhkhkhkh TID:2045 hjhjhkhkhkh I need to show only lines containing TID:0903 asdfasldjflsdjf TID:0945 hjhjhkhkhkh TID:2045 hjhjhkhkhkh TID:2045 hjhjhkhkhkh ... (11 Replies)
Discussion started by: saifurshaon
11 Replies

8. Shell Programming and Scripting

Cut lines before keyword

Hi, How to cut all lines in file before keyword? from 1 2333214 word ...... some text 2 234343 234234 word ...... some text 3 234324 324 3234 word ...... some text to 1 2333214 2 234343 234234 3 234324 324 3234 (4 Replies)
Discussion started by: Trump
4 Replies

9. UNIX for Dummies Questions & Answers

Grep and display n lines after the match is found.

Hello, How do I use grep to find a pattern in a list of file and then display 5 lines after the pattern is matched Eg: I want to match the string GetPresentCode in all files in a folder and then see 4 lines following this match. I am not sure if grep is what should be used to achieve. Thanks!... (3 Replies)
Discussion started by: cv_pan
3 Replies

10. AIX

grep to give how many times each lines were found

Lets say I have a file containing string patterns to be looked for inside a file. I would normaly do : grep -if MyFilePattern FiletoSearchInto if I use the -c it gives how many total lines it found out of my whole pattern file, but what if i want grep to report how many times it found each... (4 Replies)
Discussion started by: Browser_ice
4 Replies
Login or Register to Ask a Question