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