LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-01-2012, 03:56 AM   #1
sunilsagar
LQ Newbie
 
Registered: Jul 2011
Posts: 29

Rep: Reputation: Disabled
grep and display the below lines in file


Hello All

I have a list a file, I want to grep a word and display rest lines after the match was found.

for example :

1. 4324
2. 1.234
3. 5467
4. 3.456
5. 2.345

So, if I grep "1.234", I want display the rest after this as :

1. 5467
2. 3.456
3. 2.345

Please advise.

Thanks
 
Old 02-01-2012, 04:28 AM   #2
micklesh
LQ Newbie
 
Registered: Sep 2010
Distribution: Debian
Posts: 2

Rep: Reputation: 0
Hi
you can try the -A option followed by the number of lines to display after the match

I think there are more ways to achieve this with awk or sed

grep -A 2 1.234

regards, michael

Last edited by micklesh; 02-01-2012 at 04:32 AM. Reason: example added
 
Old 02-01-2012, 04:29 AM   #3
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 189Reputation: 189
Grep, im not so sure of.
But with sed.
Code:
~/tmp $ cat tmp
4324
1.234
5467
3.456
2.345
~/tmp $ sed '1,/1\.234/d' tmp
5467
3.456
2.345
I have taken your "1." to be indicative of line numbers, not being contained in the file itself. If they are, and you want to renumber them in the output, you will need help from a better sedder than I

Last edited by fukawi1; 02-01-2012 at 04:30 AM. Reason: additional info
 
Old 02-01-2012, 09:43 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.


I believe the A,B,C options in grep are only able to print fixed numbers of lines, and can't be told to go "to the end".


fukawi1's sed command is probably the best way to go. I'll also add that a gnu extension lets you use "0" instead of "1", to ensure that the address range works when the pattern appears on the first line.


Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

The sed faq is especially helpful for cases like this.
 
1 members found this post helpful.
  


Reply

Tags
awk, grep, sed


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grep and store two lines in other file jkeertir Linux - Newbie 6 02-16-2011 07:32 PM
[SOLVED] grep ??? searching a file and outputting 5 lines either side... redhatuser1 Linux - Newbie 2 09-29-2010 03:45 AM
How to display a file, omitting lines that contain a string? Thelionroars Linux - Newbie 12 01-22-2010 07:03 AM
Using CAT / GREP to remove a set of lines from a file wesgarner Linux - Newbie 13 11-14-2009 10:41 AM
script to grep lines out of a file qipman Linux - General 5 09-26-2007 12:01 PM


All times are GMT -5. The time now is 03:13 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration