home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / question / 10256 < prev    next >
Encoding:
Text File  |  1992-08-21  |  1.7 KB  |  50 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!news.acns.nwu.edu!casbah.acns.nwu.edu!navarra
  3. From: navarra@casbah.acns.nwu.edu (John Navarra)
  4. Subject: Passing shell variable in if statement in awk
  5. Message-ID: <1992Aug21.114503.18212@news.acns.nwu.edu>
  6. Sender: usenet@news.acns.nwu.edu (Usenet on news.acns)
  7. Organization: Northwestern University, Evanston Illinois.
  8. Date: Fri, 21 Aug 1992 11:45:03 GMT
  9. Lines: 39
  10.  
  11. I have written the following awk script called extract which prints
  12. out the lines of text in a file between pattern1 and pattern2 where
  13. the pattern is specified on the command line. Here is the script:
  14.  
  15. #!/bin/sh
  16. # extract [-n] pattern1 pattern2 < filename
  17. # awk script which prints out block of information between pattern1 and
  18. # pattern2 in a file. If pattern2 is not specified, it prints from the 
  19. # first pattern to the end of file. The -n option will print out the
  20. # line numbers of the file between the two patterns. 
  21.  
  22. if [ "$1" = "-n" ] 
  23.    then  shownumbers=yes
  24.      shift; pattern1=$1; pattern2=${2:-NF} 
  25.    else 
  26.          pattern1=$1; pattern2=${2:-NF}
  27. fi
  28.           
  29. awk '
  30. /'"$pattern1"'/,/'"$pattern2"'/ {
  31.     if ( '"$shownumbers"' )
  32.     printf("[%d] \t %s \n", NR, $0)
  33.     else
  34.     printf("%s\n",$0)
  35. }' -
  36.  
  37.     I am having trouble passing the 'shownumbers' variable into the
  38. awk script. I want to test if it exists or not. If extract is called with
  39. the -n option, the line numbers for the file are printed in addition to
  40. the line themselves (like grep -n ). If the -n is called, I set shownumbers
  41. to yes. However, it does not pass through to the awk script for some
  42. reason that I can't understand. What am I doing wrong?
  43.  
  44. -tms 
  45.  
  46. -- 
  47. From the Lab of the MaD ScIenTiST:
  48.                     
  49. navarra@casbah.acns.nwu.edu        
  50.