LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   grep and display the below lines in file (http://www.linuxquestions.org/questions/programming-9/grep-and-display-the-below-lines-in-file-926877/)

sunilsagar 02-01-2012 03:56 AM

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

micklesh 02-01-2012 04:28 AM

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

fukawi1 02-01-2012 04:29 AM

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 :p

David the H. 02-01-2012 09:43 AM

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.


All times are GMT -5. The time now is 01:18 AM.