Would like to print 3 lines after a regular expression is found in the logfile


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Would like to print 3 lines after a regular expression is found in the logfile
# 1  
Old 08-05-2011
Would like to print 3 lines after a regular expression is found in the logfile

I would like to print 3 lines after a regular expression is found in the logfile. I'm using the following code:
Code:
grep -n "$reg_exp" file.txt |while read LINE ;do i=$(echo $LINE |cut -d':' -f1 ) ;sed -n "$i,$(($i+3))p" file.txt ;done

The above code things works fine,but sometimes gives erroneous results. In my case i'm searching for the expression X104 in the logfile. Sometimes X104 will contain only 1 line to print and after printing that .It also prints the next 3 lines after that, e.g:

(i)11/02/09 02:21:15 X104
Text : Unable to unlink/erase file
System Error Number: 13 (d) - The data is invalid.

(ii) Text : X104 disabled.

(iii) 11/03/09 01:01:03 web200
Text : Freespace on C: is at a critical level (694.55MB).
Text : X104 enabled per scheduled_reboot.
NSLOOKUP was successful. Please reload your
configuration.

(i) : I get the desired results, regexp is found and the next 3 lines are printed
(ii) : regexp is matched (contains only 1 line) and printed, but I receive the next 3 lines (including the newline) which I find not useful for me.

Can anyone help me out

Last edited by Franklin52; 08-06-2011 at 05:49 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 08-05-2011
GNU grep:
Code:
grep -A3

# 3  
Old 08-05-2011
I'm using Solaris 8 and that option is not available with grep
# 4  
Old 08-05-2011
Hi,

Test next 'perl' command:
Code:
$ perl -ne 'BEGIN { $lines_to_print = 3; $regexp = "X104"; $i = 1 } do { print unless /^\s*$/ ; ++$i ; next } if ( /(?i:$regexp)/ .. ( /^\s*$/ || $i >= $lines_to_print ) ) ; $i = 1' infile
(i)11/02/09 02:21:15 X104
Text : Unable to unlink/erase file
System Error Number: 13 (d) - The data is invalid.
(ii) Text : X104 disabled.
Text : X104 enabled per scheduled_reboot.
NSLOOKUP was successful. Please reload your

Regards,
Birei
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular expression to match multiple lines?

Using a regular expression, I would like multiple lines to be matched. By default, a period (.) matches any character except newline. However, (?s) and /s modifiers are supposed to force . to accept a newline and to match any character including a newline. However, the following two perl... (4 Replies)
Discussion started by: LessNux
4 Replies

2. Shell Programming and Scripting

regular expression grouping across multiple lines

cat book.txt book1 price 23 sku 1234 auth Bill book2 sku 1233 price 22 auth John book3 auth Frank price 24 book4 price 25 sku 129 auth Tod import re f = open('book.txt', 'r') text = f.read() f.close() m =... (2 Replies)
Discussion started by: chirish
2 Replies

3. UNIX for Dummies Questions & Answers

Finding lines with a regular expression, replacing them with blank lines

So the tag for this forum says all newbies welcome... All I want to do is go through my file and find lines which contain a given string of characters then replace these with a blank line. I really tried to find a simple command to do this but failed. Here's what I did come up with though: ... (2 Replies)
Discussion started by: Golpette
2 Replies

4. UNIX for Dummies Questions & Answers

delete lines matching a regular expression

I have a very large file (over 700 million lines) that has some lines that I need to delete. An example of 5 lines of the file: HS4_80:8:2303:19153:193032 153 k80:138891 HS4_80:8:2105:5544:43174 89 k88:81949 165 k88:81949 323 0 * = 323 0 ... (6 Replies)
Discussion started by: pathunkathunk
6 Replies

5. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

6. Shell Programming and Scripting

How to print the extended regular expression ?

Hello, How to print the field separator in awk? please see the following code: cat a.txt a 1s 2s 3s 4s b 2s 4s $ awk 'BEGIN{FS==" "} {print $2 $3 }' te 1s2s 2s4s I want to get the following output : 1s 2s 2s 4s How to realize this ? $ cat te a 1s,,2s 3s ... (11 Replies)
Discussion started by: 915086731
11 Replies

7. Shell Programming and Scripting

How to continue numbering after a regular expression has been found

Hello, I have a file starting with: fixedStep chrom=chrX start=1 step=1 0.930 0.955 0.972 0.985 0.993 0.995 0.994 0.990 0.984 0.971 0.942 0.944 0.971 fixedStep chrom=chrX start=200 step=1 0.987 (2 Replies)
Discussion started by: jpoldot
2 Replies

8. Shell Programming and Scripting

sed not printing lines before a regular expression.

Hey, I found a way to print the lines which is just before a regular expression, not including the expression. sed -n '/regexp/{n;p;}' myfile Now I'm looking for a way to print all lines, exept the regular expression and also the line before the same regular expression. Use code tags. (1 Reply)
Discussion started by: Livio
1 Replies

9. Shell Programming and Scripting

Regular expression in grep -E | awk print

Hi All, I have file.txt with contents like this: random text To: recipient@email.co.uk <HTML>S7randomtext more random text random text To: recip@smtpemail.com <HTML>E5randomtext more random text random text I need the output to look like this: 1,,,1,S7 1,,,1,E5 My code so... (9 Replies)
Discussion started by: terry2009
9 Replies

10. Shell Programming and Scripting

regular expression across some lines

I am trying to use regular expression to identify ONLY the commands that hasn't the word "tablespace" within it. a command starts with "create table" and ends with ; (semicolon) example file: create table first tablespace ; create table second ( BBL_CUSTOMER_NAME VARCHAR2(32), a... (7 Replies)
Discussion started by: ynixon
7 Replies
Login or Register to Ask a Question