Get lines in 5 seconds


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get lines in 5 seconds
# 15  
Old 06-20-2005
Sorry, in my previous post I did not mention that youhave to read without waiting for the opertion to complete.
# 16  
Old 06-20-2005
Try this kind of approach:

create a shell script like this (/tmp/filename is the example file name)
Code:
#!/bin/ksh
# test.sh

old="0"
while true
do
     echo "`last_written /tmp/filename EXACTO  $old`" | read old lines         
     if [ $old -lt 0 ] ; then
         echo "Error opening file"
         exit
     fi
     echo "$lines lines found `date +%c` "    
     sleep 5
done

compile this:
Code:
/******************************************************************
*
* last_written.c  
*  usage: last_written <filename> <search string> <last byte>
*                      
*                      
*  output  <total bytes searched> <total new lines with search string>
*  assumes:
*           searchstr occurs 0 or 1 times in a line only
*           filename is a carriage control file
*  prints -1 -1 to stdout on failure 
*******************************************************************/

#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>

void usage(void)
{
    fprintf(stdout,"-1 -1 \n");
    fprintf(stderr," usage: last_written <filename> <search string> <last byte>	\n");
    exit(EXIT_FAILURE);
}

/* file close routine */
void closeit(int fd)
{
    if (close(fd)== (-1))
    {
    	fprintf(stdout,"-1 -1 \n");    	
        perror("Error opening input file");
        exit(EXIT_FAILURE);
    }	
}

/* count occurrences of searchstr in a buffer */
int find(char *buf, char *searchstr)
{
    int result = 0;
    char *p=NULL;
    
    p=strstr(buf,searchstr);
    while(p!=NULL)
    {        
        result++;
        p++;
        p=strchr(p,'\n');
        if(p!=NULL)
        {
        	p=strstr(p,searchstr);
        }
    }
    return result;
}

/* read nbytes from a file */
ssize_t readall(int fd, void *buf, size_t nbyte)
{
    ssize_t nread = 0, n=0; 

    do 
    {
        if ((n = read(fd, &((char *)buf)[nread], nbyte - nread)) == -1) 
        {
            if (errno == EINTR)
            {
                continue;
            }
            else
            {
                return (-1);
            }
        }
        if (n == 0)
        {
            return nread;
        }
        nread += n;
    } while (nread < nbyte);
    return nread;
}


int main(int argc, char *argv[])
{
    int fd=0;                      /* file descriptor */
    struct stat st;                /* stat structure for file size */  
    char *p=NULL;                  /* pointer into the file */
    char *buf=NULL;                /* holds file */
    ssize_t bytes=0;               /* bytes read from the file */
    long file_pos=0;               /* starting offset into file */
    long lines=0;                  /* number of new lines to search */
    long result=0;                 /* number of lines with searchstr */
    
    if(argc!=4) usage();          /* exit on bad parameters */
    
    fd=open(argv[1],O_RDONLY);
    if( fd< 0 || fstat(fd,&st) == (-1) )
    {
        fprintf(stdout,"-1 -1\n");  /* -1 is error */
        perror("Error opening input file");
        exit(EXIT_FAILURE);
    }    
    if(st.st_size==atol(argv[3]) ) /* old file size == new file size */
    {
        fprintf(stdout,"%10s %10d\n", argv[3],0);
        exit(EXIT_SUCCESS);
    }
    buf=malloc(st.st_size+ 1);     /*create a buffer to hold the file */
    if(buf==NULL)
    {
    	fprintf(stdout,"-1 -1 \n");
        perror("Error allocating memory");
        exit(EXIT_FAILURE);
    }
    memset(buf,0x0,st.st_size+1);
    bytes=readall(fd,buf,st.st_size);
    if(bytes==(-1))
    {
    	fprintf(stdout,"-1 -1 \n");    	
        perror("Error reading file");
        exit(EXIT_FAILURE);
    }
        
    /* start looking where we left off before */
    p=buf;
    p+=atol(argv[3]);
    bytes=st.st_size;
    if(*p)
    {                           
        result=find(p,argv[2]); /*count the number of lines with searchstr */
    }
    closeit(fd);
    free(buf);
    /*  output  <total bytes searched so far> <total new lines with search string> */
    fprintf(stdout,"%10d %10d\n",bytes,result);
    return 0;
}

into an executable image named last_written. I get this output from the script running a file stream writer in the background:

Code:
kcsdev:/home/jmcnama> lastw.sh
7 lines found Mon Jun 20 12:47:06 2005
2 lines found Mon Jun 20 12:47:11 2005
2 lines found Mon Jun 20 12:47:16 2005
1 lines found Mon Jun 20 12:47:21 2005
2 lines found Mon Jun 20 12:47:26 2005
1 lines found Mon Jun 20 12:47:31 2005
3 lines found Mon Jun 20 12:47:36 2005

# 17  
Old 06-20-2005
Try using "tail +n".

#!/bin/ksh
# test.sh

old=0
while true
do
tail +$old /yourdir/yourfile > /tmp/temp_file
lines=grep -c EXACTO /tmp/temp_file
old=$old+`wc -l /tmp/temp_file'
echo "$lines lines found `date +%c` "
sleep 5
done
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sorting and wc -l w.r.t seconds

I have file with below data 00:00:00 00:00:00 00:00:00 00:00:01 00:00:01 00:00:01 00:00:01 00:00:01 00:02:01 00:02:01 00:02:01 so on till 23:59:59 I want count of seconds for each hour and minutes say for 00:00:00 its 3 and 00:00:01 its 5 and 00:02:01 its 3 and so on... (8 Replies)
Discussion started by: mirwasim
8 Replies

2. Shell Programming and Scripting

datetime difference in seconds

Hi, I'm trying to find processing time of my script. Please can someone give me the commands to get the start/end time in "dd-mm-yyyy hh:mm:ss" and the differnce in seconds. Thanks! (5 Replies)
Discussion started by: dvah
5 Replies

3. UNIX for Dummies Questions & Answers

Number of leap seconds

Is there a function call in std library or unit command that returns the number of current leap seconds? GG (4 Replies)
Discussion started by: NAVTime
4 Replies

4. Shell Programming and Scripting

How to delay the process for few seconds

Hi, In my shell script, (as per the requirement), I am creating few files, and the processes are launched parallelly . (by using "&" at the end of the command line). As per the logic, I need to remove these files as well, after creating. But, the problem is, due to parallel processing,... (3 Replies)
Discussion started by: jitendriya.dash
3 Replies

5. Shell Programming and Scripting

Convert Seconds to hh:mm:ss

Hi All I need to convert a number of fields in a record from seconds to hh:mm:ss ( or possibly hhh:mm:ss ). I'm guessing awk is the way to go . File has multiple records and each record contains 101 fields - can awk handle that ? The seconds values will be in fields 3 - 101 and could be 0. ... (4 Replies)
Discussion started by: Mudshark
4 Replies

6. HP-UX

Ticks in seconds.

Hello all, Is there any thumb rule or aproximation of the equivalence in second of one tick? Thank you in advance. (1 Reply)
Discussion started by: mig28mx
1 Replies

7. UNIX for Advanced & Expert Users

Time Difference in seconds

It is required to calculate time difference in seconds between epoch time (19700101 00:00:00) and any given date time (e.g. 20010214 14:30:30). Is there any command in unix to get it? Thanks in adv. (1 Reply)
Discussion started by: k_bijitesh
1 Replies

8. UNIX for Dummies Questions & Answers

seconds to hh:mm:ss

Any sleek way to convert seconds to hh:mm:ss format . I know it can be done by mod and divide . Looking for a one liner if possible . Example 3600 seconds = 01:00:00 3601 seconds = 01:00:01 (2 Replies)
Discussion started by: akrathi
2 Replies

9. UNIX for Advanced & Expert Users

how to get number of seconds

How do I get the number of seconds since 1970, within a script, for the previous day at 23:59? I need this value to pass into a sql statement to cleanup records older than the previous day at midnight. It will be automated via cron so no hard coding allowed. Thanks! (2 Replies)
Discussion started by: captainzeb
2 Replies
Login or Register to Ask a Question