home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.shell
- Path: sparky!uunet!uunet.ca!canrem!telly!robohack!ve3ied!bduncan
- From: bduncan@ve3ied.UUCP (Bill Duncan)
- Subject: Re: searching for 1st occurrence
- Organization: TFMCS
- Date: Fri, 14 Aug 1992 01:21:56 GMT
- Message-ID: <1992Aug14.012156.5499@ve3ied.UUCP>
- References: <15m8aqINN6ci@seven-up.East.Sun.COM>
- Lines: 44
-
- In article <15m8aqINN6ci@seven-up.East.Sun.COM> ltaylor@blinky.East.Sun.COM (Laura Taylor) writes:
- >Inside a shellscript, I would like to search for only the
- >first occurrence of a particular string, or in otherwords,
- >just one occurrence of the same string so I can set it to
- >a variable.
- >
- >For example, say in file called systems, there is a list of
- >all the system architectures something like...
- >
- >SS2
- >SS1
- >SS2
- >4/75
- >
- >I'd like to use say grep such that when I search for SS2, it
- >only grabs one occurrence of SS2, and does not spit out SS2 twice.
- >
- >Anyone got any hints?
- >
- >Thanks,
- >
- >/laura
-
- This is the kind of thing awk's associative arrays make easy.
- I'll show you an abridged version of what I use for finding out *just*
- the last time everyone has logged in:
-
- last | awk '
- {
- if (!($1 in SeenIt)) {
- SeenIt[ $1 ] = 1 # just store anything here, eg. line itself etc.
- print
- }
- }'
-
- You can of store other data in the associative arrays if you need to.
- Since we don't need to here, I just store a "1" as a placeholder. In
- fact I think an empty string will work just as well.
-
- Hope this helps.
- Regards,
-
- Bill Duncan (bduncan@ve3ied.UUCP)
-
-