[KLUG Programming] sorting, uniq'ing, and grepping

Tony Gettig programming@kalamazoolinux.org
Mon, 14 Jul 2003 10:11:21 -0400


Hi folks,

I've got a process that I am trying to convert to a C program. In short, I 
need to combine two files into one, sort the resulting file, grab the unique 
lines, then pull out certain bits. I currently accomplish it this way (psuedo 
code):

cat file1 > newfile
cat file2 >> newfile
sort newfile > sortfile
uniq -uw7 sortfile > uniqfile
grep criteria1 uniqfile > importfile
grep criteria2 uniqfile > removefile

I then take the importfile and removefile and process them into LDIF, batch, 
and CSV files. I think I handle that part line by line once I sort, uniq, and 
grep.

I can combine two files into one. No prob there. Bash makes the rest of it 
really easy, but my goal is to make a C program that I can compile to run on 
any number architectures...Linux, Unix, NetWare, or (gasp) DOS. I don't know 
that I want or need to create all those files in my C program. But if it is 
easier to handle one file at a time, then maybe I should. 

I've got the file combining working. It compiles on all the platforms listed 
above using different compilers. (side note...the free OpenWatcom compiler 
works great for NetWare NLM's.)

Am I nuts? Should I just stick to the shell? That would limit where this 
process could be run from and by who. Or maybe something like this would work:

myprogram combines the files then...
shell out to sort, uniq, & grep to files...
myprogram picks up from there

Can you tell I'm not a full time programmer? :)

-- 
Tony Gettig
Voiceovers, PGP key, and more at
http://gettig.net