Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines.
Places a line containing -- between contiguous groups of
matches.
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
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?
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.
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):
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.