home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / question / 13101 < prev    next >
Encoding:
Text File  |  1992-11-08  |  1.6 KB  |  45 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!coplex!trebor!root
  3. From: root@trebor.uucp (Bob Stockler)
  4. Subject: Re: Separate tabs from blanks ? 
  5. Organization: Bob Stockler
  6. Date: Sat, 07 Nov 1992 19:28:30 GMT
  7. Message-ID: <1992Nov07.192830.8629@trebor.uucp>
  8. References: <1992Nov7.005823.7082@leland.Stanford.EDU>
  9. Lines: 34
  10.  
  11. peer@ccrma.stanford.edu (Peer Landa) writes:
  12.  
  13. >Is there any text processing filter which separates tabs from blanks ?
  14. >Also, is it possible in AWK to print a previous (or second previous) line after
  15. >a specific word is find on the current line ??
  16.  
  17. I'm not sure I understand your first question, so I'll ignore it.
  18.  
  19. The answer to your second question is "Yes".  Here's a Bourne shell script
  20. I wrote about 5 years ago to give the functionality of a "grep-in-context".
  21. Knowing what I do now, I could improve upon it, but the code in it should
  22. serve well enough as an example for you to do what you asked about:
  23.  
  24. :
  25. # @(#) cgrep.awk
  26. usage="usage:  $0 'string ...' filenames"
  27. [ "$1" ] && { strings=$1 ; shift ;} || { echo $usage ; exit 1 ;}
  28. [ "$1" ] && files="$@" || files=*
  29. for i in $strings ; do pattern="$pattern$fs$i" ; fs=\| ; done
  30. awk '
  31. BEGIN          { prev1 = prev2 = "" }
  32. /'"$pattern"'/ { printf( "\nFile: %s - Line: %d\n\n", FILENAME, NR )
  33.                  printf( "%s\n%s\n%s\n", prev1, prev2, $0 )
  34.                  if ( getline ) { print ; prev1 = prev2 ; prev2 = $0
  35.                    if ( getline ) print 
  36.                  }
  37.                }
  38.                { prev1 = prev2 ; prev2 = $0 }
  39. ' $files
  40. exit
  41. # EOF 'cgrep.awk'
  42.  
  43. On casually looking at it now, it's more like an "egrep-in-context".
  44.  
  45.