home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!news.acns.nwu.edu!casbah.acns.nwu.edu!navarra
- From: navarra@casbah.acns.nwu.edu (John Navarra)
- Subject: Passing shell variable in if statement in awk
- Message-ID: <1992Aug21.114503.18212@news.acns.nwu.edu>
- Sender: usenet@news.acns.nwu.edu (Usenet on news.acns)
- Organization: Northwestern University, Evanston Illinois.
- Date: Fri, 21 Aug 1992 11:45:03 GMT
- Lines: 39
-
- I have written the following awk script called extract which prints
- out the lines of text in a file between pattern1 and pattern2 where
- the pattern is specified on the command line. Here is the script:
-
- #!/bin/sh
- # extract [-n] pattern1 pattern2 < filename
- # awk script which prints out block of information between pattern1 and
- # pattern2 in a file. If pattern2 is not specified, it prints from the
- # first pattern to the end of file. The -n option will print out the
- # line numbers of the file between the two patterns.
-
- if [ "$1" = "-n" ]
- then shownumbers=yes
- shift; pattern1=$1; pattern2=${2:-NF}
- else
- pattern1=$1; pattern2=${2:-NF}
- fi
-
- awk '
- /'"$pattern1"'/,/'"$pattern2"'/ {
- if ( '"$shownumbers"' )
- printf("[%d] \t %s \n", NR, $0)
- else
- printf("%s\n",$0)
- }' -
-
- I am having trouble passing the 'shownumbers' variable into the
- awk script. I want to test if it exists or not. If extract is called with
- the -n option, the line numbers for the file are printed in addition to
- the line themselves (like grep -n ). If the -n is called, I set shownumbers
- to yes. However, it does not pass through to the awk script for some
- reason that I can't understand. What am I doing wrong?
-
- -tms
-
- --
- From the Lab of the MaD ScIenTiST:
-
- navarra@casbah.acns.nwu.edu
-