:x,y!perl -ne'print "$. $_";' |
20061024
vi Number Sequence Trick
I'm a vim user.
Often times in my editing, I want to number some of the lines in a file from 1 to some ending number. It might be only a few lines, or it might be thousands. To save myself some carpal tunnel, I wrote one line of perl to do the job. This program will pass a block of lines in whatever file you are editing to a one line perl script which will number the lines starting with 1 and drop that text back into the editor:
In this example, x is the starting line number and y is the ending line number. That's it!
20061015
File Character Frequency
In my machine learning course, our first project was to write a program to sort e-mail into nine class labels A through I. In my implementation after every e-mail was classified, I would write a single character to the screen. There were several thousand of e-mails that needed sorting, so I needed a program that displayed a final tally of the results.
I could have easily built something into the program I was writing, but I thought something more general purpose would be useful. I quickly wrote the following code. It takes a tally of each character in a file and then uses a quick sort to sort the data and then prints the results. I even added a switch to ignore whitespace characters.
#include <stdio.h>
/* Author: James Church
* Date: 09/11/06
* Program: charfreq
* Version: 0.3
* Description: Takes a single file from the command line as input and
* reports the character frequence of each character in order
* from most common to least common.
*
* Copyright © 2006 Free Software Foundation, Inc.
*
* Copying and distribution of this file, with or without modification,
* are permitted in any medium without royalty provided the copyright
* notice and this notice are preserved.
*/
#define CHARRANGE 256
typedef struct _charcount {
unsigned char c;
long count;
} CharCount;
void quicksort (CharCount *a, int i, int j);
int partition (CharCount *a, int i, int j);
int main(int argc, char **argv) {
int i;
unsigned char c;
CharCount freq[CHARRANGE];
FILE *file;
int ignore_whitespace = 0;
int argparse = 1;
if (argc == 1) {
printf("\nUsage: %s [-s] [filename] - Reports character frequence of file.\n", argv[0]);
printf(" -s Ignores whitespace (optional)\n");
return 0;
} // End If
if (strcmp("-s", argv[argparse]) == 0) {
ignore_whitespace = 1;
argparse++;
} // End If
if ((file = fopen(argv[argparse], "r")) == NULL) {
printf("Error: Cannot open %s\n", argv[1]);
return 0;
} // End If
argparse++;
for (i = 0; i < CHARRANGE; i++) {
freq[i].c = (unsigned char) i;
freq[i].count = 0;
} // End For
while (1) {
fread (&c, sizeof(unsigned char), 1, file);
if (feof(file)) break;
if (ignore_whitespace && (c == ' ' || c == '\t' || c == '\r' || c == '\n'))
continue;
freq[c].count++;
} // End While
quicksort(freq, 0, CHARRANGE-1);
for (i = CHARRANGE-1; i >= 0; i--) {
if (freq[i].count == 0) break;
printf("%c[%3d]: %d\n", freq[i].c, freq[i].c, freq[i].count);
} // End For
fclose(file);
return 0;
} // End Main
void quicksort (CharCount *a, int i, int j) {
int p;
if (i < j) {
p = partition (a, i, j);
quicksort (a, i, p-1);
quicksort (a, p+1, j);
} /* End If */
} /* End mergesort */
int partition (CharCount *a, int i, int j) {
int val = a[i].count;
int h = i;
int k;
CharCount temp;
for (k = i+1; k <= j; k++)
if (a[k].count < val) {
h++;
temp = a[h];
a[h] = a[k];
a[k] = temp;
} /* End If */
temp = a[i];
a[i] = a[h];
a[h] = temp;
return h;
} /* End partition */ |
Enjoy!
20061012
Finding Identical Files using bash and find
I wanted to start a blog of all the little bits of code featuring tricks that I've learned over the years. I don't know how often I'll update this blog, but any time I use a bit of code to complete a complicated task, I'll describe the task and show the code used to solve it.
My first example will be a identical file finder. In my class on Java programming that I teach, I suspected two students of turning in the exact same work. As I was grading, I had seen some identical code elsewhere, but I couldn't remember. I keep all the students work in different directories on my office computer just in case I need to look at their old homework solutions. Rather than look at every solution manually for an identical file, I wrote this little bash script to do the work for me:
This script "findIdent" takes two arguments: a file pattern (say... "*.mp3") and the file that you want to see if duplicates exist.
So did I find any duplicates of students' work? No. Turns out I just found some very similar code and it was nothing to worry about. But I kept this script in case I ever need it again.
#!/bin/bash
if [ $# -eq 2 ]
then
for i in $(find . -name $1); do diff -s $i $2 | grep -v differ; done
else
echo "USAGE: findIdent [SOME FILE EXPRESSION] [SOME FILE]"
fi |
Subscribe to:
Comments (Atom)
