home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / shell / 3491 < prev    next >
Encoding:
Text File  |  1992-08-14  |  1.5 KB  |  55 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!uunet.ca!canrem!telly!robohack!ve3ied!bduncan
  3. From: bduncan@ve3ied.UUCP (Bill Duncan)
  4. Subject: Re: searching for 1st occurrence
  5. Organization: TFMCS
  6. Date: Fri, 14 Aug 1992 01:21:56 GMT
  7. Message-ID: <1992Aug14.012156.5499@ve3ied.UUCP>
  8. References: <15m8aqINN6ci@seven-up.East.Sun.COM>
  9. Lines: 44
  10.  
  11. In article <15m8aqINN6ci@seven-up.East.Sun.COM> ltaylor@blinky.East.Sun.COM (Laura Taylor) writes:
  12. >Inside a shellscript, I would like to search for only the
  13. >first occurrence of a particular string, or in otherwords,
  14. >just one occurrence of the same string so I can set it to
  15. >a variable.
  16. >
  17. >For example, say in file called systems, there is a list of
  18. >all the system architectures something like...
  19. >
  20. >SS2
  21. >SS1
  22. >SS2
  23. >4/75
  24. >
  25. >I'd like to use say grep such that when I search for SS2, it
  26. >only grabs one occurrence of SS2, and does not spit out SS2 twice.
  27. >
  28. >Anyone got any hints?
  29. >
  30. >Thanks,
  31. >
  32. >/laura 
  33.  
  34. This is the kind of thing awk's associative arrays make easy.
  35. I'll show you an abridged version of what I use for finding out *just*
  36. the last time everyone has logged in:
  37.  
  38. last | awk '
  39. {
  40.     if (!($1 in SeenIt)) {
  41.         SeenIt[ $1 ] = 1 # just store anything here, eg. line itself etc.
  42.         print
  43.     }
  44. }'
  45.  
  46. You can of store other data in the associative arrays if you need to.
  47. Since we don't need to here, I just store a "1" as a placeholder.  In
  48. fact I think an empty string will work just as well.
  49.  
  50. Hope this helps.
  51. Regards,
  52.  
  53. Bill Duncan (bduncan@ve3ied.UUCP)
  54.  
  55.