LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-09-2009, 03:28 PM   #1
Poki
LQ Newbie
 
Registered: Apr 2009
Posts: 21

Rep: Reputation: 0
how to print 30 lines after regex


Hi all
Please help me find a way to print 30 lines after regex match

tx
Poki
 
Old 06-09-2009, 03:41 PM   #2
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 70
What command are you currently using? And have you tried a man page? Or even asking your teacher?
 
Old 06-10-2009, 10:20 AM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Please read man grep:

Code:
       -A NUM, --after-context=NUM
              Print NUM  lines  of  trailing  context  after  matching  lines.
              Places  a  line  containing  --  between  contiguous  groups  of
              matches.
 
Old 06-10-2009, 11:58 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Homework??

Note that you never posted any follow-up in your other thread.
 
Old 06-11-2009, 09:42 AM   #5
Poki
LQ Newbie
 
Registered: Apr 2009
Posts: 21

Original Poster
Rep: Reputation: 0
Thank you all for your valuable replies, i know very well how to use man pages, and no it is not a homework, i tried grep -A 30 it didn't work, that is the reason why i posted question

And regarding my previous post, none of the suggestions worked, so i didn't really need to thank anybody
 
Old 06-11-2009, 10:13 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
No, you don't need to thank anybody if you don't want, but you need to explain the problem in details, since the grep -A option is the straightforward way to print N lines after a match and if it does not work for you, the problem must be addressed in a different way.

Questions:
- what exact command have you tried?
- what error message do you get?
- can you provide a real example?
 
Old 06-11-2009, 10:38 AM   #7
Poki
LQ Newbie
 
Registered: Apr 2009
Posts: 21

Original Poster
Rep: Reputation: 0
You are probably right, i will redefine it:

i need to print n lines, between line number x and y, but x and y are variables so the easy way to do it with sed or awk don't work, because they accept numbers for line numbers and not variables.


sed -n '22531,22534p' /mylog

i need something like this:

sed -n '$fromLine,$toLinep' /mylog unfortunately this doesn't work

so my question is there a way to print n lines according to the range stored in variables.


Thank you
Poki
 
Old 06-11-2009, 10:43 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
This issue is slightly different from the original one, since you're not trying to match a regexp anymore, right? The sed command fails because the single quotes prevent the shell variable substitution. You have to use double quotes or even no quotes at all. Also embed the name of the variable in brackets, otherwise $toLinep is treated as a variable name (included the p):
Code:
sed -n ${fromLine},${toLine}p /mylog
 
Old 06-11-2009, 10:57 AM   #9
Poki
LQ Newbie
 
Registered: Apr 2009
Posts: 21

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
This issue is slightly different from the original one, since you're not trying to match a regexp anymore, right? The sed command fails because the single quotes prevent the shell variable substitution. You have to use double quotes or even no quotes at all. Also embed the name of the variable in brackets, otherwise $toLinep is treated as a variable name (included the p):
Code:
sed -n ${fromLine},${toLine}p /mylog


Thank you so much, it worked, i can finally print n lines according to variables, i googled so much for this answer.

First i thought i want to print n lines after i find regex, but then i reformulated it, because i found a way to find line numbers according to which i need to print log entries.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed command to print matching lines and 2 lines above.. DX398 Programming 12 10-01-2008 08:25 AM
grep regex . matches new lines?! lambchops468 Linux - Newbie 3 03-24-2008 09:19 PM
print the file: between the lines viveksnv Programming 3 02-26-2008 03:06 PM
Regex Question: Only print part of line that matches TheMeteorPolice Programming 5 01-12-2006 01:21 PM
Searching for 2 empty lines by RegEx in perl mosh Programming 8 09-08-2004 02:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration