Shell Programming Question - sort
Brad Appleton
brad at SSD.CSD.HARRIS.COM
Wed Mar 7 03:42:07 AEST 1990
Sorry to post this to the net but I couldnt successfully send mail!
In article <751 at ncs.dnd.ca> Gordon Marwood writes:
>I am trying to sort (using "sort" in Ultrix) based on the last two
>characters in a line (which are numeric). There are a variable number
>of characters in a line, and these last two characters are preceded by a
>space, there are also a variable number of spaces in a line, so the
>number of fields will be variable if space is used as the field
>separator. None of my available texts gives me a clue as to whether a
>sort can be done based on the last field in a line, regardless of the
>number of fields in the line. Is there any "sort" option that can
>do this ?
>
>Gordon Marwood
>Internet: marwood at ncs.dnd.ca
If I knew what you are trying to achieve (with sort) I might be able
to provide better assistance! All I can suggest is:
1) see if you can get to the last field using $NF in awk
2) (probably better than #1 ...)
I believe that BSD Unix should have a command called "reverse" which
reverses the characters in each line of a file. Run your file through
reverse, then sort, then back through reverse (this may not work
depending upon how fancy a sort you need to do).
reverse file | sort [options] | reverse [-] > outfile
Just in case you dont have reverse... It is easy to write! The following
did just fine for me on Xenix (it is not a superlatively written piece of
code but it performs a simple job, and it works (on Xenix anyway :-):
------------------cut here----------------------cut here----------------
/**
* reverse.c -- C source to reverse the characters in the lines of one
* or more files.
*
* NAME
* reverse -- reverse the characters in each line of input
*
* SYNOPSIS
* reverse [-]|[ filename ... ]
*
* DESCRIPTION
* Reverse will each line of input and print the resultant line on
* the standard output. If "-" is given as a filename, then input is
* taken from the standard input. Actually, "-" may be listed as one
* of several filenames and, at that time, stdin will be used for input
* (after the previous files) and then will continue with the remaining
* files. I have not tried this out however!
*
* Created Mar '89 by Brad Appleton
*
* Mar 8 '90, Brad Appleton -- made same minor additions of #defines and
* subroutines in order to not require my own
* personal .h files
**/
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#define USE_STDIN "-"
#define LINE_LEN 512 /* make this as big as you need */
#include <stdio.h>
/* ckopen -- open file; check for success */
FILE *ckopen( filename, filemode ) char *filename, *filemode;
{
FILE *fopen(), *fp;
if ( (fp = fopen( filename, filemode )) == NULL ) {
fprintf( stderr, "reverse: unable to open %s\n", filename );
exit( 2 );
}/* if */
return (fp);
}/* ckopen() */
/* reverse -- reverse the chars in a string (but not the newline) */
void reverse( str ) char *str;
{
char hold;
int i, j, append = FALSE, len = strlen( str );
if ( str[ len -1 ] == '\n' ) {
str[ --len ] = '\0';
append = TRUE;
} /* if new-line */
for ( i = 0, j = (len -1 ) ; i <= j ; i++, j-- ) {
/* swap( str[i], str[j] ) */
hold = str[i];
str[i] = str[j];
str[j] = hold;
}/* for */
if ( append ) str[ len ] = '\n';
}/* reverse() */
main( argc, argv ) int argc; char *argv[];
{
int i;
char line[ LINE_LEN ];
FILE *infile;
if ( argc == 1 ) { /* print usage and exit */
fprintf( stderr, "usage: remind [-]|[filename ...]\n" );
exit( 1 );
}/* if no args */
/* process each file in the order given on the command line */
for ( i = 1 ; i < argc ; i++ ) {
if ( ! strcmp( argv[i], USE_STDIN ) )
infile = stdin;
else
infile = ckopen( argv[i], "r" );
while ( fgets( line, LINE_LEN, infile ) != NULL ) {
reverse( line );
fputs( line, stdout );
}/* while */
}/* for each arg */
exit( 0 );
}/* main */
----------------finish cut---------------------finish cut-----------------------
+=-=-=-=-=-=-=-=-= "... and miles to go before I sleep." -=-=-=-=-=-=-=-=-=-+
| Brad Appleton | Harris Computer Systems Division |
| | 2101 West Cypress Creek Road |
| brad at ssd.csd.harris.com | Fort Lauderdale, FL 33309 USA |
| ... {uunet | novavax}!hcx1!brad | MailStop 161 (305) 973-5007 |
+=-=-=-=-=-=-=-=- DISCLAIMER: I said it, not my company! -=-=-=-=-=-=-=-=-=-+
More information about the Comp.unix.questions
mailing list