home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / question / 10279 < prev    next >
Encoding:
Internet Message Format  |  1992-08-21  |  3.2 KB

  1. Path: sparky!uunet!cs.utexas.edu!swrinde!mips!darwin.sura.net!uvaarpa!concert!sas!mozart.unx.sas.com!foster!sherman
  2. From: sherman@unx.sas.com (Chris Sherman)
  3. Newsgroups: comp.unix.questions
  4. Subject: Re: Passing shell variable in if statement in awk
  5. Message-ID: <sherman.714444204@foster>
  6. Date: 22 Aug 92 00:43:24 GMT
  7. References: <1992Aug21.114503.18212@news.acns.nwu.edu>
  8. Sender: news@unx.sas.com (Noter of Newsworthy Events)
  9. Organization: SAS Institute Inc.
  10. Lines: 80
  11. Nntp-Posting-Host: foster.unx.sas.com
  12.  
  13. In <1992Aug21.114503.18212@news.acns.nwu.edu> navarra@casbah.acns.nwu.edu (John Navarra) writes:
  14.  
  15. >I have written the following awk script called extract which prints
  16. >out the lines of text in a file between pattern1 and pattern2 where
  17. >the pattern is specified on the command line. Here is the script:
  18.  
  19.  
  20. >#!/bin/sh
  21. ># extract [-n] pattern1 pattern2 < filename
  22. ># awk script which prints out block of information between pattern1 and
  23. ># pattern2 in a file. If pattern2 is not specified, it prints from the 
  24. ># first pattern to the end of file. The -n option will print out the
  25. ># line numbers of the file between the two patterns. 
  26.  
  27. >if [ "$1" = "-n" ] 
  28. >   then  shownumbers=yes
  29. >     shift; pattern1=$1; pattern2=${2:-NF} 
  30. >   else 
  31. >         pattern1=$1; pattern2=${2:-NF}
  32. >fi
  33. >          
  34. >awk '
  35. >/'"$pattern1"'/,/'"$pattern2"'/ {
  36. >    if ( '"$shownumbers"' )
  37. >    printf("[%d] \t %s \n", NR, $0)
  38. >    else
  39. >    printf("%s\n",$0)
  40. >}' -
  41.  
  42. >    I am having trouble passing the 'shownumbers' variable into the
  43. >awk script. I want to test if it exists or not. If extract is called with
  44. >the -n option, the line numbers for the file are printed in addition to
  45. >the line themselves (like grep -n ). If the -n is called, I set shownumbers
  46. >to yes. However, it does not pass through to the awk script for some
  47. >reason that I can't understand. What am I doing wrong?
  48.  
  49.  
  50. No variable replacements happen inside single-quoted sections of code.
  51. Above, where you do an awk ' blah blah ', nothing gets replaced inside.
  52.  
  53. So we have to do it another way.  Here's one alternative...
  54. Note:  Notice that I had to back-slash the $0's, because otherwise they
  55. would have been replaced too.
  56.  
  57.  
  58. ----------------------------------------------------------------------------
  59. #!/bin/sh
  60. # extract [-n] pattern1 pattern2 < filename
  61. # awk script which prints out block of information between pattern1 and
  62. # pattern2 in a file. If pattern2 is not specified, it prints from the
  63. # first pattern to the end of file. The -n option will print out the
  64. # line numbers of the file between the two patterns.
  65.  
  66. if [ "$1" = "-n" ]
  67.    then  shownumbers=1
  68.          shift; pattern1=$1; pattern2=${2:-NF}
  69.    else
  70.          pattern1=$1; pattern2=${2:-NF}
  71.      shownumbers=0
  72.  
  73. fi
  74.  
  75. cat > /tmp/awk.$$ << --EndOfScript--
  76. /$pattern1/,/$pattern2/ {
  77.     if ( $shownumbers  )
  78.       printf("[%d] \t %s \n", NR, \$0)
  79.     else
  80.       printf("%s\n", \$0)
  81. --EndOfScript--
  82. awk -f /tmp/awk.$$
  83. rm /tmp/awk.$$
  84. ----------------------------------------------------------------------------
  85.  
  86. Hope this helps...
  87. -- 
  88.      ____/     /     /     __  /    _  _/    ____/
  89.     /         /     /     /   /      /     /          Chris Sherman
  90.    /         ___   /        _/      /          /
  91.  _____/   __/   __/   __/ _\    _____/   _____/           sherman@unx.sas.com
  92.