Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!shelby!apple!bloom-beacon!tut.cis.ohio-state.edu!emory!tridom!wht From: wht@tridom.uucp (Warren Tucker) Newsgroups: alt.sources Subject: cgname - collect file names from {e,f,}grep output Keywords: egrep fgrep grep Message-ID: <130@tridom.uucp> Date: 23 Jun 89 22:21:16 GMT Organization: Tridom Corporation, Atlanta Lines: 113 This quickie collects the filenames produced by grep output while passing the output oin to stdout. I wrote it to help me build a list of filenames to edit containing a certain string: usage: {e,f,}grep | cgname [| ...]\n"); collects list of files reported by grep into \n"); cgname reads grep output, passing it on to cgname's stdout\n"); and produces a sorted list of filenames into \n"); i.e.: fgrep BLAH *.[ch] | cgname /tmp/123 vi `cat /tmp/123` {awk,sed,perl}-fanatic flames > /dev/null /*+------------------------------------------------------------------------- cgname.c - pass thru {e,f,}grep output, collecting filenames ...!gatech!emory!tridom!wht --------------------------------------------------------------------------*/ /*+:EDITS:*/ /*:06-23-1989-17:34-wht-creation */ #include #define ff fprintf #define se stderr FILE *fptmp; #if defined(M_XENIX) | defined(USG) | defined(SYSV) /* etc */ #define Strchr strchr #else #define Strchr index /* BSD */ #endif char *Strchr(); /*+------------------------------------------------------------------------- collect_name(grepstr) --------------------------------------------------------------------------*/ int collect_name(grepstr) register char *grepstr; { register char *cptr; if(!(cptr = Strchr(grepstr,':'))) return(1); fwrite(grepstr,1,(int)(cptr - grepstr),fptmp); fputc('\n',fptmp); return(0); } /* end of collect_name */ /*+------------------------------------------------------------------------- main(argc,argv,envp) --------------------------------------------------------------------------*/ main(argc,argv,envp) int argc; char **argv; char **envp; { register found_output = 0; char tmpname[32]; char s256[256]; if((argc == 1) || (*argv[1] == '-')) { ff(se,"usage: {e,f,}grep | cgname [| ...]\n"); ff(se,"collects list of files reported by grep into \n"); ff(se,"cgname reads grep output, passing it on to cgname's stdout\n"); ff(se,"and produces a sorted list of filenames into \n"); exit(1); } sprintf(tmpname,"/tmp/c%05d.tmp",getpid()); if((fptmp = fopen(tmpname,"w")) == NULL) { perror("cgname: cannot open temp file"); exit(1); } while(fgets(s256,sizeof(s256),stdin)) { if(collect_name(s256)) { fputs( "cgname: missing filename from grep output (only 1 file?)\n",se); found_output = 0; break; } found_output = 1; fputs(s256,stdout); } fclose(fptmp); if(found_output) { sprintf(s256,"sort -u %s > %s",tmpname,argv[1]); system(s256); } unlink(tmpfile); exit(!found_output); } /* end of main */ /* end of cgname.c */ -- ------------------------------------------------------------------- Warren Tucker, Tridom Corporation ...!gatech!emory!tridom!wht Sforzando (It., sfohr-tsahn'-doh). A direction to perform the tone or chord with special stress, or marked and sudden emphasis.