Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 02-01-2012, 03:56 AM   #1
Registered: Jul 2011
Posts: 30

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.

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

Rep: Reputation: 0
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
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 191Reputation: 191
Grep, im not so sure of.
But with sed.
~/tmp $ cat tmp
~/tmp $ sed '1,/1\.234/d' tmp
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 + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.

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


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

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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:41 PM.

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