Re: Best unix programming language or tool for dealing with plaintext
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Re: Best unix programming language or tool for dealing with plaintext




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
11-17-05 11: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





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 04:42 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register