/* Program to keep a fraction of the number of lines detailed in the input file list. Kept lines are written to the output file. Fraction should be >0.0 and <1.0 Typically the input file would contain a list of file names destined to be migrated (or not) between Lustre OST. A random number is thrown and lines are kept if the normalized random is <= the desired fraction. */ #include #include #include #include int main(int argc, char *argv[]) { char fullLine[1024]; int skipLines; int totalLines; int outputLines; char *cPnt; FILE *f, *o; int tooShort; double fraction, keepThese; time_t tTime, thisTime; if( argc != 4 ) { printf ("%d is the wrong number of arguments\n", argc-1); printf (" Please supply: input_file output_file fraction\n"); exit(1); } // Check the fraction for validity keepThese = atof(argv[3]); if ( keepThese <= 0.0 || keepThese > 1.0 ) { printf (" Abort: keep fraction %f is out of range\n", keepThese); exit(1); } // Initialize random numbers, etc time(&tTime); thisTime = mktime(localtime(&tTime)); srand((unsigned int) thisTime); skipLines=0; totalLines=0; outputLines=0; f = fopen(argv[1], "r"); o = fopen(argv[2], "w"); while(1) { cPnt = fgets( fullLine, 512, f ); if ( cPnt == NULL ) break; totalLines++; // pLast = cPnt + strlen(fullLine); tooShort = rand(); fraction = ((10000.0*tooShort)/RAND_MAX)/10000.0; if ( fraction > keepThese ) { skipLines++; continue; } fputs(fullLine, o); outputLines++; } fclose(f); fclose(o); printf( "%d lines read\n%d lines output\n%d lines dropped\n", totalLines, outputLines, skipLines); exit(0); }