Grep string but also it will show the next 5 lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep string but also it will show the next 5 lines
# 1  
Old 10-07-2008
Grep string but also it will show the next 5 lines

Hi experts,

I want to grep a number 9366109380 from a file but it will also show me the next 5 lines. Below is the example-

when i grep 989366109380, i can also see the next 5 lines.

Line 1. <fullOperation>MAKE:NUMBER:9366109380:PPAY2;</fullOperation>
Line 2. <starttime>20081003000047</starttime>
Line 3. <stoptime>20081003000047</stoptime>
Line 4. <fullResult>940120105;;</fullResult>
Line 5. <status>FAILED</status>

Please help..

//Purple
# 2  
Old 10-07-2008
First, try GNU "grep -A 5 989366109380". If you don't have and can't get GNU grep (fileutils package), then try:

Code:
awk '/989366109380/,printed==5 { ++printed; print; }'

There are probably other solutions with sed.
# 3  
Old 10-07-2008
awk '/989366109380/,printed==5 { ++printed; print; }'

above not giving any output Smilie

pleaese tell me other way??
# 4  
Old 10-07-2008
1. Your sample input gave a different number than the one I used. Did you use the right number??
2. Did you provide an input filename?
# 5  
Old 10-07-2008
I guess you mean printed==4; also, the example has 9366109380 (remove the first two digits from the awk script)
# 6  
Old 10-07-2008
check the -A option of grep..Example shown below...Grepped on "bash" keyword and printed next 5 lines.

Code:
[root@iqcharlie nua7]# grep -A 5 bash create_current.conf 
#!/bin/bash
echo "Please enter the hostname"
read hostname
echo "Please enter the ipaddress"
read ip
echo "Please enter the DB server name"

# 7  
Old 10-07-2008
Quote:
Originally Posted by era
I guess you mean printed==4; also, the example has 9366109380 (remove the first two digits from the awk script)
Heh, his example showed the next four lines, but he wrote the next 5. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep three consecutive lines if each lines contains certain string

say we have : 2914 | REQUEST | whatever 2914 | RESPONSE | whatever 2914 | SUCCESS | whatever 2985 | RESPONSE | whatever 2986 | REQUEST | whatever 2990 | REQUEST | whatever 2985 | RESPONSE | whatever 2996 | REQUEST | whatever 2010 | SUCCESS | whatever 2013 | REQUEST | whatever 2013 |... (7 Replies)
Discussion started by: Saumitra Pandey
7 Replies

2. Shell Programming and Scripting

Grep a string and count following lines starting with another string

I have a large dataset with following structure; C 0001 Carbon D SAR001 methane D SAR002 ethane D SAR003 propane D SAR004 butane D SAR005 pentane C 0002 Hydrogen C 0003 Nitrogen C 0004 Oxygen D SAR011 ozone D SAR012 super oxide C 0005 Sulphur D SAR013... (3 Replies)
Discussion started by: Syeda Sumayya
3 Replies

3. Shell Programming and Scripting

Grep into a file + show following lines

Hi guys, This is probably very easy but I've no idea how to pull this out. Basically, I need to find errors into a very large logfile. When you grep the ID, the output is like this: +- Type: 799911 Code: Ret: 22728954 Mand: X Def: Des: UserDes: SeqNo: 2 +- Type: 799911 Code: Ret:... (5 Replies)
Discussion started by: Arkadia
5 Replies

4. Shell Programming and Scripting

Grep couple of consecutive lines if each lines contains certain string

Hello, I want to extract from a file like : 20120530025502914 | REQUEST | whatever 20120530025502968 | RESPONSE | whatever 20120530025502985 | RESPONSE | whatever 20120530025502996 | REQUEST | whatever 20120530025503013 | REQUEST | whatever 20120530025503045 | RESPONSE | whatever I want... (14 Replies)
Discussion started by: black_fender
14 Replies

5. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

6. Shell Programming and Scripting

Print lines between two lines after grep for a text string

I have several very large file that are extracts from Oracle tables. These files are formatted in XML type syntax with multiple entries like: <ROW> some information more information </ROW> I want to grep for some words, then print all lines between <ROW> AND </ROW>. Can this be done with AWK?... (7 Replies)
Discussion started by: jbruce
7 Replies

7. Shell Programming and Scripting

grep to show date/time of file the string was found in.

I've seen several examples of grep showing the filename the string was found in, but what I really need is grep to show the file details in long format (like ls -l would). scenario is: grep mobile_number todays_files This will show me the string I'm after & which files they turn up in, but... (2 Replies)
Discussion started by: woodstock
2 Replies

8. Shell Programming and Scripting

grep string & a few lines after

i need to grep a STRING_A & the next few lines after the STRING_A example file: STRING_A yada yada line 1 line 2 STRING_B yada yada line 1 line 2 line 3 STRING_A yada yada line 1 line 2 line 3 line 4 STRING_A yada yada line 1 line 2 line 3 line 4 (7 Replies)
Discussion started by: ashterix
7 Replies

9. Shell Programming and Scripting

grep to show lines only after pattern

When i grep for a pattern the search results comes up with matching lines(some before the pattern and some after)...how can i limit the search so that it shows only the lines after the pattern specified (5 Replies)
Discussion started by: wannalearn
5 Replies

10. UNIX for Dummies Questions & Answers

How can you show lines surrounding a search string?

I would like to be able to grep (or some such thing) a search argument and then display the line plus the preceding 3 lines of the file and the following 3 lines of the file. Any ideas? Thanks in advance! :D (3 Replies)
Discussion started by: robster
3 Replies
Login or Register to Ask a Question