Question

I would like to see first 5 lines after the match found in vi editor. What is the command?

I only know these commands

/Incident Id: Gives all lines from first to last

:g/Incident ID: gives only the match word hiding all non match

But how to show first 5 lines after the match word found... ?


Example with expected output :

i have text of 8 lines, & i search for Incident ID

    Incident ID: 1392875740716  <<<Match word here
    URL: /Project/jsps/ErrorPage.jsp
    java.lang.NullPointerException
            at java.util.Calendar.setTime(Calendar.java:1092)
            at com.cando.restaurant.utils.DateRange.createForWeek(DateRange.java:54)
            at org.springframework.transaction.interceptor.TransactionInterceptor :110)
            at org.springframework.aop.framework.ReflectiveMethodInvocation 
            at org.springframework.aop.interceptor.ExposeInvocationInterceptor 

Expected output

  Incident ID: 1392875740716  <<<Match word here
    URL: /Project/jsps/ErrorPage.jsp
    java.lang.NullPointerException
            at java.util.Calendar.setTime(Calendar.java:1092)
            at com.cando.restaurant.utils.DateRange.createForWeek(DateRange.java:54)
Was it helpful?

Solution 2

I'm not sure what you mean by "only first 5 lines after the match found" as vi will display an entire file.

If you want to only see this output you could use grep instead of vi

$ grep -A 5 'Incident Id:' <your input file>

OTHER TIPS

Try this

:g/Incident ID/#5

Also see :help :g, :help :p and :help :z (that's if you have vim, not vi).

Although it is not specifically a vim command but you can try it on any file from vim editor. Vim also allows you to execute a command directly from the editor, without needing to drop to a shell, by using bang (!) followed by the command to be run. So:

:!grep -A 5 "Incident ID" filename.txt

from grep man pages :

-A NUM, --after-context=NUM Print NUM lines of trailing context after matching lines. Places a line containing -- between contiguous groups of matches.

-B NUM, --before-context=NUM Print NUM lines of leading context before matching lines. Places a line containing -- between contiguous groups of matches.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top