Unix Programming - Re: Best unix programming language or tool for dealing with plaintext

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > November 2005 > Re: Best unix programming language or tool for dealing with plaintext





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Re: Best unix programming language or tool for dealing with plaintext
J.F. Cornwall

2005-11-17, 6:13 pm

Friedrich Dominicus wrote:
> Friedrich Dominicus <just-for-news-frido@q-software-solutions.de> writes:
>
>
>
> 6250)' < cars.txt
>
> sorry for the follow up to myself a thing I forgot. You can use C for
> it of course also, I guess hardly anyone will suggest it for
> scriptint, but believe it or not a lot of tools started their live as
> "hackerisch" C solution.
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <pcre.h>
>
>
> int main(void){
> char buf[256];
> FILE *fp = NULL;
> int rval, matched;
> pcre *re;
> const char *err_ptr;
> int err_offset;
> int ovector[30];
> int year, price;
> char scratch[50];
>
> fp = fopen("cars.txt", "r");
> if (NULL == fp){
> fprintf(stderr, "Could not open file, giving up\n");
> exit(EXIT_FAILURE);
> }
>
> re = pcre_compile("\\w+\\W+\\w+\\W+(\\d+)\\W+(\\d+)", 0, &err_ptr, &err_offset, NULL);
> if (NULL == re){
> fprintf(stderr, "Could not compile regular expression, err: %s", err_ptr);
> pcre_free((char*)err_ptr);
> exit (EXIT_FAILURE);
> }
> while (fgets(buf, sizeof(buf), fp) != NULL){
> matched = pcre_exec(re, NULL, buf, strlen(buf), 0, 0, ovector, 30);
> if (matched >= 0) {
> rval = pcre_copy_substring(buf, ovector, matched, 1, scratch, 50);
> year = atol(scratch);
> rval = pcre_copy_substring(buf, ovector, matched, 2, scratch, 50);
> price = atol(scratch);
> if (year >= 1991 && price < 6250){
> printf("%s", buf);
> }
> }
> }
> pcre_free(re);
> fclose(fp);
> return 0;
> }
>
>
> If you feel it feel not terrible good, well be it as is. It depends
> all what you may gain on the other hand. If you want to make yourself
> comfortable with C this code might be a good thing to learn. If you
> think: "Oh my good it's C", it's probably not what you want.
>
> Regards
> Friedrich


Or this solution (no rigorous error handling included because I was too
lazy this time around):

program readcardata
character*15 make, model
integer year, cost
open (unit=5,file='./cardata')
do
read (5,*,end=666) make, model, year, cost
if ((year .ge. 1991) .and. (cost .lt. 6250)) print *,
make,model,year,cost
end do
666 stop
end program readcardata

{jcorn} readdnelnc: % f90 -o readcardata readcardata.f90
{jcorn} readdnelnc: % readcardata
ford fiesta 1991, 4575
honda accord 1991, 6000
STOP
{jcorn} readdnelnc: %

Fortran works pretty nice for text as well as number crunching. YMMV of
course. :-)

Jim
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com