Advertisement
Guest User

Simulate `grep -C 3 regex'

a guest
Jun 30th, 2011
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 0.51 KB | None | 0 0
  1. BEGIN {
  2.     lines = 3
  3.     buffer = lines + 1
  4.     n = lines + 2
  5.     i = j = 0
  6.     separate = 2
  7. }
  8.  
  9. /regex/ {
  10.     if (lines && separate == 1)
  11.         print "--"
  12.     while (i != j) {
  13.         print a[i++]
  14.         if (i == buffer)
  15.             i = 0
  16.     }
  17.     n = separate = 0
  18. }
  19.  
  20. n > lines {
  21.     a[j++] = $0
  22.     if (j == buffer)
  23.        j = 0
  24.     if (i == j) {
  25.         i++
  26.         if (i == buffer)
  27.             i = 0
  28.         if (!separate)
  29.             separate++
  30.     }
  31.     next
  32. }
  33.  
  34. n++ <= lines
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement