Cshell question: taking wordlists as a single string

Randal Schwartz merlyn at iwarp.intel.com
Thu Aug 16 16:59:17 AEST 1990


In article <3251 at syma.sussex.ac.uk>, andy at syma (Andy Clews) writes:
| I have a Csh script called "whatnews" which takes words as arguments and
| searches the file /usr/lib/news/newsgroups for lines containing any one
| of those words, the requirement being to show users what newsgroups are
| there that may be something to do with the searched word.  It is not
| case sensitive (i.e.  it uses fgrep -i)
| 
| 	whatnews biology
| 
| "whatnews" then proceeds to show me all lines in /usr/lib/news/newgroups
| that contain the string "biology".  If several words are supplied as a
| list, whatnews searches the file for each of these words.  The
| difficulty arises because I want to do (for example)
| 
| 	whatnews "bug reports"
| 
| where the intended effect is to search out all lines containing the
| string "bug reports". At the moment it splits this up into "bug" and
| "reports" and does two searches. This is because the script contains a
|       foreach i ($*)
| loop for repeated searches.  Quoting (single or double) doesn't help.
| 
| Basically, then, can Cshell cope with word-lists as single arguments, or
| must I write a C program to do the job (or try sh or ksh?)
| 
| No joy as yet with TFM. Any help appreciated; email probably best. I
| will summarise if possible. Thanks for listening.

Well, you can try this Perl script:

================================================== snip snip
#!/usr/bin/perl
for (@ARGV) {
	s/\W/\\$1/g; # de-magicize
	$all .= "$_|"; # and create regular-expression
}
chop($all); # remove last "|"

open(G,"/usr/lib/news/newsgroups") || die "Cannot open newsgroups: $!";
while (<G>) {
	print if /$all/io;
}
close(G);
================================================== snip snip

Untested, but it should do the job.
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn at iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/



More information about the Comp.unix.questions mailing list