Login or Register to Ask a Question and Join Our Community


Grep into a file + show following lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep into a file + show following lines
# 1  
Old 04-15-2013
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:

Code:
 +- Type: 799911 Code:  Ret: 22728954 Mand: X Def:  Des:  UserDes:  SeqNo: 2
  +- Type: 799911 Code:  Ret: 22728954 Mand: X Def:  Des:  UserDes:  SeqNo: 2
  +- Type: 799911 Code:  Ret: 22728954 Mand: X Def:  Des:  UserDes:  SeqNo: 2
  +- Type: 799911 Code:  Ret: 22728954 Mand: X Def:  Des:  UserDes:  SeqNo: 2

Those are multiple occurrences of the same action.
What I need to do is to take only one of these results (because they are identical) and show the next 30 lines in the file, starting from the grep line, to highlight the error.

Is it doable?
Thank you Smilie
# 2  
Old 04-15-2013
Code:
grep ID file | sort -u

Code:
awk '/ID/ && !A[$0]++' file

# 3  
Old 04-15-2013
Use -A option to grep, if supported.

If -A option is not available, there are ways using sed or awk.
# 4  
Old 04-15-2013
If there are extra spaces between the fields, I guess pamu's suggestion needs a bit modification.

$1=$1 forces records to be reconstituted:
Code:
awk '/ID/{$1=$1}!A[$0]++' file

# 5  
Old 04-15-2013
Code:
awk '$0 ~ EXPR { N=5 } (N--)>0' EXPR="texttomatch" inputfile

# 6  
Old 04-16-2013
Code:
awk '$0 ~ PAT { LPr = NR + NL } LPr > NR' PAT="pattern" NL=30 file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep: show only first match per line?

Hi, can I make grep stop after finding the first occurrence of a regex in a line? Given: file with various regex patterns file to be grep'ed Sadly some of the regex patterns cannot be limited any further, so grep -Eiof patterns.txt file.txt (GNU grep 2.20) will give me possibly n hits... (4 Replies)
Discussion started by: stresing
4 Replies

2. UNIX for Advanced & Expert Users

Grep show file name once

I am using grep as follows grep --include \*.org -ir "sunspot" -C 3 ./astron_aphys/solarsy/sun/helioseism/localhs/fhankel/ This gives me the filename for each matched line. How can I change the command to print the file name only once rather than having the same file name repeated at... (2 Replies)
Discussion started by: kristinu
2 Replies

3. Shell Programming and Scripting

sed show lines text between 2 blank lines

I have a file like blah blah blah blah this is the text I need, which might be between 1-4 lines, but always has a blank line above and below it, and is at the end of the text file the code tags don't show the trailing blank line. I started by deleting the last blank line with: ... (2 Replies)
Discussion started by: unclecameron
2 Replies

4. Shell Programming and Scripting

[Solved] Show lines above and below!

hello team - anyone can help shwoing how to grep a file showing the lines above and below? Thx Bragabio (7 Replies)
Discussion started by: bragabio
7 Replies

5. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

6. Shell Programming and Scripting

Grep command to show only process name

Can I modify the grep command to show only a process name? Currently I run ps -efa | grep chk_web to get the following: mousr 3395 1 0 09:36:06 pts/10 0:00 sh /var/opt/scripts/chk_web.sh Can this be changed in any way to get only: /var/opt/scripts/chk_web.sh or chk_web.sh. I... (3 Replies)
Discussion started by: runnerpaul
3 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 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.... (10 Replies)
Discussion started by: thepurple
10 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

Need ls to show number of lines in each file

Hi, I need a command that would let ls show number of lines in each file rather than file size in KBs. I tried using wc -l as a source of input to ls but I found a problem cutting the file name since wc generates a space delimited list. Any suggestions? Thanks. GmMike. (1 Reply)
Discussion started by: GMMike
1 Replies
Login or Register to Ask a Question

Featured Tech Videos