LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-31-2009, 07:08 AM   #1
markotitel
Member
 
Registered: Feb 2009
Location: Titel - Serbia
Posts: 181

Rep: Reputation: 18
How to grep more than one line from a file


Hi,

Is it possibile somehow to grep some text from a file and then show N number of columns starting from the "grepped" text ?

For example I have a text

Code:
1234
asdf
qwer
zxcv
1234
And I want to 'grep qwer' , and I want to see 3 lines after QWER line also.
 
Old 08-31-2009, 07:10 AM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 404Reputation: 404Reputation: 404Reputation: 404Reputation: 404
See man grep, and look for "-n"
 
Old 08-31-2009, 07:14 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
see grep -A option.
 
Old 08-31-2009, 07:33 AM   #4
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Rep: Reputation: 36
grep -Ax <keyword> <filename>

this will grep for the keyword and print x lines after that.


grep -Bx <keyword> < filename>

will grep for the keybword and print x lines before the keyword.
 
Old 08-31-2009, 07:52 AM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 404Reputation: 404Reputation: 404Reputation: 404Reputation: 404
-nx will do both.
 
Old 08-31-2009, 08:12 AM   #6
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Rep: Reputation: 36
^^^^ sweet never knew it
 
Old 08-31-2009, 08:28 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
-n prefix each line output with a number, -x match whole line. I don't think -nx does what OP wants...or am i missing something.?
 
Old 08-31-2009, 08:39 AM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 404Reputation: 404Reputation: 404Reputation: 404Reputation: 404
Quote:
Originally Posted by ghostdog74 View Post
-n prefix each line output with a number, -x match whole line. I don't think -nx does what OP wants...or am i missing something.?
Just try it.

-n alone does that. -n with a number is -like -Ax -Bx together, though it also adds the line number.

Last edited by i92guboj; 08-31-2009 at 08:40 AM.
 
Old 08-31-2009, 08:52 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by i92guboj View Post
Just try it.

-n alone does that. -n with a number is -like -Ax -Bx together, though it also adds the line number.
ah... you are denoting "x" in -nx as a number, not the actual -x option. if that's the case, yes, -n<num> can also be used.
 
Old 08-31-2009, 09:03 AM   #10
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 404Reputation: 404Reputation: 404Reputation: 404Reputation: 404
Quote:
Originally Posted by ghostdog74 View Post
ah... you are denoting "x" in -nx as a number, not the actual -x option. if that's the case, yes, -n<num> can also be used.
Yes, I should have been more clear. But as the other user above used that notation I tried not to confuse the OP by adding yet another different one.
 
Old 09-03-2009, 06:33 AM   #11
markotitel
Member
 
Registered: Feb 2009
Location: Titel - Serbia
Posts: 181

Original Poster
Rep: Reputation: 18
Thank you very much
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] grep line above and below poiuytrez Programming 6 07-04-2009 08:27 PM
shell script find a line and the next line (grep?) metalx1000 Programming 5 07-24-2007 08:41 PM
Grep pattern first line of a file ericcarlson Linux - Newbie 11 07-20-2004 10:51 AM
grep a line in a file from dir bkeating Linux - Newbie 4 07-24-2003 02:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:02 PM.

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