search a file between two begin and end strings in c

Can any one help me out with following problem...

I want to search in a file which has two strings repeat each time(like start and end) i want to search between these two string in C programming.
please help me with the solution.

thanks in advance.

I think it's close to bulletproof:

$ cat between.c
#include <stdio.h>

int main(void)
{
        const char *start="START", *end="END";

        while(!feof(stdin))
        {
                int c, spos=0;

                while((c=fgetc(stdin)) >= 0)
                {
                        // If it doesn't match, ignore it and start over.
                        if(c != start[spos])
                        {
                                spos=0;
                                // if it doesn't match start[n], it might
                                // match start[0], so check again.
                                if(c != start[0])
                                        continue;
                        }

                        // Increment position in the START string
                        // and check if we found a complete START.
                        if(start[++spos] == '\0') // complete START
                                break;
                }

                if(c<0) break; // No START, just EOF

                spos=0;
                // Search for END
                while((c=fgetc(stdin)) >= 0)
                {
                        if(c != end[spos])
                        {
                                int n;
                                // This time, we have to print everything
                                // we can prove didn't match.
                                fwrite(end, spos, 1, stdout);
                                spos=0;
                                if(c != end[0])
                                {
                                        fputc(c, stdout);
                                        continue;
                                }
                        }

                        if(end[++spos] == '\0') // complete END
                        {
                                // print a newline after each END is found
                                fputc('\n', stdout);
                                break;
                        }
                }
        }

        return(0);
}
$ gcc between.c
$ echo "STSTART A END B START C ENEND D" | ./a.out
 A
 C EN
$
1 Like

I have a file with the following content i need to get the Timestamp,Elapsetime, and calculate the averages of the Elapsedtime, for perticulaer process and need to display the process name, the process is NEWOLUF2

BEGINTRANS
logging.c/LogTransactionInput (3105) : Timestamp <14Sep1111-07-37> Input <NEWOLUF2!NEWOLUF2<?xml version="1.0" standalone="yes
"?><OLUF2><INPUT><FLT>SK 1467</FLT><LEG>OSLCPH</LEG><DEPDATE>27SEP11</DEPDATE><BUCKET>Y</BUCKET><BUCKETBKGCL>Y</BUCKETBKGCL><B
KGCL>Y</BKGCL><PARMS>Y</PARMS><COMMENTS>Y</COMMENTS><WARNINGS>Y</WARNINGS><OPARAMETERS>Y</OPARAMETERS><LOCALAVAPOS>Y</LOCALAVA
POS></INPUT></OLUF2>>
ElapseTime <745> milliseconds
ENDTRANS
BEGINTRANS
logging.c/LogTransactionInput (3105) : Timestamp <14Sep1111-07-37> Input <NEWOLUF2!NEWOLUF2<?xml version="1.0" standalone="yes
"?><OLUF2><INPUT><FLT>SK 1455</FLT><LEG>OSLCPH</LEG><DEPDATE>29SEP11</DEPDATE><BUCKET>Y</BUCKET><BUCKETBKGCL>Y</BUCKETBKGCL><B
KGCL>Y</BKGCL><PARMS>Y</PARMS><COMMENTS>Y</COMMENTS><WARNINGS>Y</WARNINGS><OPARAMETERS>Y</OPARAMETERS><LOCALAVAPOS>Y</LOCALAVA
POS></INPUT></OLUF2>>
ElapseTime <765> milliseconds
ENDTRANS
BEGINTRANS
logging.c/LogTransactionInput (3105) : Timestamp <14Sep1111-07-37> Input <<NAVSREQUEST><FLT>SK 4417 </FLT><LEG>TOSOSL</LEG><DE
PDATE>02SEP12</DEPDATE></NAVSREQUEST>>
ElapseTime <579> milliseconds
ENDTRANS
BEGINTRANS
logging.c/LogTransactionInput (3105) : Timestamp <14Sep1111-07-38> Input <<NAVSREQUEST><FLT>SK 1467 </FLT><LEG>OSLCPH</LEG><DE
PDATE>27SEP11</DEPDATE></NAVSREQUEST>>
ElapseTime <356> milliseconds
ENDTRANS
BEGINTRANS
logging.c/LogTransactionInput (3105) : Timestamp <14Sep1111-07-38> Input <<NAVSREQUEST><FLT>SK 1455 </FLT><LEG>OSLCPH</LEG><DE
PDATE>29SEP11</DEPDATE></NAVSREQUEST>>
ElapseTime <390> milliseconds
ENDTRANS
BEGINTRANS
logging.c/LogTransactionInput (3105) : Timestamp <14Sep1111-07-41> Input <DEPLEGS!1!CMD=0#STAT=0#MAXREC= 8#FLT=SK 1467 #DEPDAT
E=18SEP11>
ElapseTime <286> milliseconds
ENDTRANS
BEGINTRANS
logging.c/LogTransactionInput (3105) : Timestamp <14Sep1111-07-41> Input <NEWOLUF2!NEWOLUF2<?xml version="1.0" standalone="yes
"?><OLUF2><INPUT><FLT>SK 1467</FLT><LEG>OSLCPH</LEG><DEPDATE>18SEP11</DEPDATE><BUCKET>Y</BUCKET><BUCKETBKGCL>Y</BUCKETBKGCL><B
KGCL>Y</BKGCL><PARMS>Y</PARMS><COMMENTS>Y</COMMENTS><WARNINGS>Y</WARNINGS><OPARAMETERS>Y</OPARAMETERS><LOCALAVAPOS>Y</LOCALAVA
POS></INPUT></OLUF2>>
ElapseTime <651> milliseconds
ENDTRANS
BEGINTRANS
logging.c/LogTransactionInput (3105) : Timestamp <14Sep1111-07-42> Input <<NAVSREQUEST><FLT>SK 1467 </FLT><LEG>OSLCPH</LEG><DE
PDATE>18SEP11</DEPDATE></NAVSREQUEST>>
ElapseTime <292> milliseconds
ENDTRANS
BEGINTRANS
logging.c/LogTransactionInput (3105) : Timestamp <14Sep1111-07-52> Input <DEPLEGS!1!CMD=0#STAT=0#MAXREC= 8#FLT=SK 0811 #DEPDAT
E=02SEP12>
ElapseTime <345> milliseconds

Why not continue this in your original thread?

This is not possible because i need to search for the NEWOLUF2 and get the Elapsetime,Timestamp for the same string but these TWO are in different lines so how can i search and get the values exactly.

Is that what the data looks like, or are the lines actually really long?

---------- Post updated at 11:06 AM ---------- Previous update was at 10:50 AM ----------

Will the process name always be NEWOLUF2?

---------- Post updated at 11:16 AM ---------- Previous update was at 11:06 AM ----------

Here's what I have so far, assuming the long lines were wrapped accidentally. It's much, much, much easier to write when you know what you're supposed to be operating on :stuck_out_tongue:

$ cat begintrans.c
#include <stdio.h>
#include <string.h>

int main(void)
{
	char buf[16384];

	int transcount=0, elapsetotal=0;

	while(!feof(stdin))
	{
		int newoluf=0, elapsetime=-1;
		char timestamp[64]={0};

		buf[0]='\0';	// Blank out buffer

		while(strcmp(buf, "BEGINTRANS\n") != 0)	// look for BEGINTRANS
		if(fgets(buf, 16384, stdin) == NULL)
			break;	// EOF

		if(feof(stdin)) break;

		while(strcmp(buf, "ENDTRANS\n") != 0)	// Look for ENDTRANS
		{
			const char *tsstr;
			if(fgets(buf, 16384, stdin) == NULL)
				break;

			if(strstr(buf, "NEWOLUF2") != NULL)
				newoluf=1;

			if(tsstr=strstr(buf, "Timestamp"))
				sscanf(tsstr, "Timestamp <%[^>]>", timestamp);

			if(strncmp(buf, "ElapseTime", 10) == 0)
				sscanf(buf, "ElapseTime <%d>", &elapsetime);
		}

		if((elapsetime>=0) && (timestamp[0]))
		{
			transcount++;
			elapsetotal += elapsetime;
			printf("trans %d, elapsetime %d, timestamp %s\n",
				transcount, elapsetime, timestamp);
		}
	}

	printf("Average elapsetime is %d\n", elapsetotal / transcount);
	return(0);
}
$ gcc begintrans.c -o begintrans
$ ./begintrans < data
trans 1, elapsetime 745, timestamp 14Sep1111-07-37
trans 2, elapsetime 765, timestamp 14Sep1111-07-37
trans 3, elapsetime 579, timestamp 14Sep1111-07-37
trans 4, elapsetime 356, timestamp 14Sep1111-07-38
trans 5, elapsetime 651, timestamp 14Sep1111-07-41
trans 6, elapsetime 292, timestamp 14Sep1111-07-42
trans 7, elapsetime 345, timestamp 14Sep1111-07-52
Average elapsetime is 533
$
1 Like

This is only for the process NEWOLUF2 after this i'll change to other process and the content is in a file from the file we have to read the data and perform the task the exact scenario is as follows..

Please write a C program that give us sufficient performance statistics on the WSodrms server.
The program name should be WSodrmsStatistics.c.

The program should do the folloiwing:

1) It should have -s -e options to read start and end of timer interval we want to measure.
2) Scan throgh the log file and pick all NEWOLUF2 tranactions from the logfile within the user defined time interval
3) Save the TimeStamp and ElapseTime in a structure, which you add to a list with AddToTailAL():

typdef struct {
char *strTransactionName[LEN_TRANS_NAME + 1];
int intTimeStamp;
int intElapseTime;
}

3) Calculate the average value over the elements in the list.
4) Print with Logging functions (LOGTYPE_NOPREFIX) the following information:
Transaction name = <strTransactionName> TimeInterval <HHMISS - HHMISS> Average Elapse Time: <XX.XX>
show the result in Seconds with 2 decimals (SS.SS)

When you start copy-pasting arbitrary numbered requirements it's pretty obvious you're posting homework.

Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.