how to use "grep" command to find a match and to print followup of 10 lines from the match. this i need to get some error statements from log files. (else need to download use match for log time and then copy the content). Instead of downloading bulk size files i need to run a command to get those number of lines.
Asked
Active
Viewed 1.7k times
1
-
possible duplicate of [grep a file, but show several surrounding lines?](http://stackoverflow.com/questions/9081/grep-a-file-but-show-several-surrounding-lines). You can't because Solaris has an antique grep, but there are a few alternatives listed here. – msw May 26 '14 at 12:14
2 Answers
8
A default install of Solaris 10 or 11 will have the /usr/sfw/bin
file tree. Gnu grep - /usr/sfw/bin/ggrep
is there. ggrep supports /usr/sfw/bin/ggrep -A 10 [pattern] [file]
which does what you want.
Solaris 9 and older may not have it. Or your system may not have been a default install. Check.

jim mcnamara
- 16,005
- 2
- 34
- 51
-
Above solution holds good for file.How do I use "ggrep" on command output? – chetan honnavile Feb 19 '19 at 23:18
1
Suppose, you have a file /etc/passwd
and want to filter user "chetan"
Please try below command:
cat /etc/passwd | /usr/sfw/bin/ggrep -A 2 'chetan'
It will print the line with letter "chetan" and the next two lines as well.
-- Tested in Solaris 10 --

Gilles Heinesch
- 2,889
- 1
- 22
- 43

Ankit Saxena
- 11
- 1
-
Tested on SunOS 5.10 and running the following: ggrep -A 2 'Script ended' *.xml – Nasri Najib Oct 14 '20 at 09:32