-   Programming (
-   -   grep and display the below lines in file (

sunilsagar 02-01-2012 04: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.


micklesh 02-01-2012 05:28 AM

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 05:29 AM

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

David the H. 02-01-2012 10: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.

The sed faq is especially helpful for cases like this.

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