Stu Asked: 2011-10-21 08:17:02 +0800 CST2011-10-21 08:17:02 +0800 CST 2011-10-21 08:17:02 +0800 CST OS/2 grep had a great feature where it would show you n lines BEFORE the search item was found. Is there an equivalent in unix anywhere? 772 Solaris would be preferable, but I'll take linux or something I can compile. Does such a beast exist? unix ibm grep 3 Answers Voted Kvisle 2011-10-21T08:19:09+08:002011-10-21T08:19:09+08:00 As far as GNU grep go, this will show you the number of lines before the match: # grep -B number Equivilent for after: # grep -A number You can download GNU Grep here: http://www.gnu.org/s/grep/ Best Answer user9517 2011-10-21T08:36:02+08:002011-10-21T08:36:02+08:00 You can get GNU grep and it's dependencies for Solaris from sunfreeware.com either as a binary download in pkg format which installs in /usr/local/bin or as a source package. wnoise 2011-10-21T10:09:02+08:002011-10-21T10:09:02+08:00 A small awk script will also work: #!/usr/bin/awk -f BEGIN { context=3; } { add_buffer($0) } /pattern/ { print_buffer() } function add_buffer(line) { buffer[NR % context]=line } function print_buffer() { for(i = max(1, NR-context+1); i <= NR; i++) { print buffer[i % context] } } function max(a,b) { if (a > b) { return a } else { return b } } replace /pattern/ with the actual regular expression or pattern to search for.
As far as GNU grep go, this will show you the number of lines before the match:
Equivilent for after:
You can download GNU Grep here: http://www.gnu.org/s/grep/
You can get GNU grep and it's dependencies for Solaris from sunfreeware.com either as a binary download in pkg format which installs in /usr/local/bin or as a source package.
A small awk script will also work:
replace
/pattern/
with the actual regular expression or pattern to search for.