home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cs.utexas.edu!swrinde!mips!darwin.sura.net!uvaarpa!concert!sas!mozart.unx.sas.com!foster!sherman
- From: sherman@unx.sas.com (Chris Sherman)
- Newsgroups: comp.unix.questions
- Subject: Re: Passing shell variable in if statement in awk
- Message-ID: <sherman.714444204@foster>
- Date: 22 Aug 92 00:43:24 GMT
- References: <1992Aug21.114503.18212@news.acns.nwu.edu>
- Sender: news@unx.sas.com (Noter of Newsworthy Events)
- Organization: SAS Institute Inc.
- Lines: 80
- Nntp-Posting-Host: foster.unx.sas.com
-
- In <1992Aug21.114503.18212@news.acns.nwu.edu> navarra@casbah.acns.nwu.edu (John Navarra) writes:
-
- >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?
-
-
- No variable replacements happen inside single-quoted sections of code.
- Above, where you do an awk ' blah blah ', nothing gets replaced inside.
-
- So we have to do it another way. Here's one alternative...
- Note: Notice that I had to back-slash the $0's, because otherwise they
- would have been replaced too.
-
-
- ----------------------------------------------------------------------------
- #!/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=1
- shift; pattern1=$1; pattern2=${2:-NF}
- else
- pattern1=$1; pattern2=${2:-NF}
- shownumbers=0
-
- fi
-
- cat > /tmp/awk.$$ << --EndOfScript--
- /$pattern1/,/$pattern2/ {
- if ( $shownumbers )
- printf("[%d] \t %s \n", NR, \$0)
- else
- printf("%s\n", \$0)
- }
- --EndOfScript--
- awk -f /tmp/awk.$$
- rm /tmp/awk.$$
- ----------------------------------------------------------------------------
-
- Hope this helps...
- --
- ____/ / / __ / _ _/ ____/
- / / / / / / / Chris Sherman
- / ___ / _/ / /
- _____/ __/ __/ __/ _\ _____/ _____/ sherman@unx.sas.com
-