/* gtfield.c
 *
 * With a string as one argument get the #th column field (-c #)
 * or the the field starting at character position (-n #)
 *
 */
#include <stdio.h>
#include <string.h>
#include  <stdfun.h> 
#include  <ascii.h> 

#define MAXLINE 514

#define OPTSTRING "c:n:"


static char _sccsid[] = { " %M% %I% %H% " };

int main( argc, argv )
int argc;
char *argv[];
{
	extern int getopt();

	char *gtfld();

	char line[ MAXLINE ], fld[ MAXLINE ];
	int nflag = 0;
	int col = 1;

	extern int optind;
	extern char *optarg;
	int c_opt;

	int k;

	while( ( c_opt = getopt( argc, argv, OPTSTRING ) ) != EOF )    {
		switch( c_opt )    {
			case 'c':
				/* Get field in column (1-16) indicated by
				 * argument; default is 1.
				 */ 
				if( sscanf( optarg, "%d", &col ) != 1 )    {
					printf( "gtfield: Bad Argument to -c\n" );
					printf( "Usage: gtfield [-c #] where #=1-16\n" );
					exit( 1 );
				}
				if( col > 16 )    {
					printf( "Usage: %s [-c #] where #=1-16\n", argv[0] );
					fprintf( stderr, "Argument to -c out of range\n" );
					exit( 1 );
				}
				break;
			case 'n':
				nflag = 1;
				/* Get field in column at absolute position
				 * argument; default is 0.
				 */ 
				if( sscanf( optarg, "%d", &col ) != 1 )    {
					printf( "%s: Bad Argument to -n\n", argv[0] );
					printf( "Usage: %s [-c # | -n #]\n", argv[0] );
					exit( 1 );
				}
				if( col > (MAXLINE - 2) || col < 0 )    {
					printf( "Usage: %s [-c #] where #=0-%d\n", argv[0], (MAXLINE - 3) );
					fprintf( stderr, "Argument to -c out of range\n" );
					exit( 1 );
				}
				break;
			default:
				printf( "%s: Bad Option\n", argv[0] );
				printf( "Usage: %s [-c # | -n #]\n", argv[0] );
				exit( 1 );
		}
	} /* End while getting options */

	for( k = optind; k < argc; k++ )    {
		sprintf( &line[ strlen( line ) ], " %s", argv[ k ] );
	}

	if( nflag == 1 )    {
		sscanf( &line[ col ], "%s", fld );
	}
	else     {
		gtfld( line, col, fld );
	}

	printf( "%s", fld );

	exit( 0 );

	return( 0 );

} /* End main() */

char *gtfld( line, col, fld )
char *line;
int col;
char *fld;
{

	switch( col )    {
		case 1:
			sscanf( line , "%s", fld );
			break;
		case 2:
			sscanf( line , "%*s%s", fld );
			break;
		case 3:
			sscanf( line , "%*s%*s%s", fld );
			break;
		case 4:
			sscanf( line , "%*s%*s%*s%s", fld );
			break;
		case 5:
			sscanf( line , "%*s%*s%*s%*s%s", fld );
			break;
		case 6:
			sscanf( line , "%*s%*s%*s%*s%*s%s", fld );
			break;
		case 7:
			sscanf( line , "%*s%*s%*s%*s%*s%*s%s", fld );
			break;
		case 8:
			sscanf( line , "%*s%*s%*s%*s%*s%*s%*s%s", fld );
			break;
		case 9:
			sscanf( line , "%*s%*s%*s%*s%*s%*s%*s%*s%s", fld );
			break;
		case 10:
			sscanf( line , "%*s%*s%*s%*s%*s%*s%*s%*s%*s%s", fld );
			break;
		case 11:
			sscanf( line , "%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%s", fld );
			break;
		case 12:
			sscanf( line , "%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%s", fld );
			break;
		case 13:
			sscanf( line , "%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%s", fld );
			break;
		case 14:
			sscanf( line , "%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%s", fld );
			break;
		case 15:
			sscanf( line , "%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%s", fld );
			break;
		case 16:
			sscanf( line , "%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%s", fld );
			break;
	}

	return( fld );
}


Return to Home Page
Return to Metayoga Page
Return to C Language Page


The URL for this document is:
http://graham.main.nc.us/~bhammel/graham/CPROGS/gtfield.html
Created: 1997
Last Updated: May 28, 2000 Email me, Bill Hammel at
bhammel@graham.main.nc.us
READ WARNING BEFORE SENDING E-MAIL