#!/bin/sh # # $Id: msort,v 1.2 1996/10/21 19:48:06 vogelke Exp $ PATH=/bin:/usr/bin:/usr/local/bin export PATH USAGE=' msort: sorts multiline records of varying lengths. Author: Susan Andarmani, January 91 UnixWorld. Usage: msort [-r line] [file] msort [-rline] [file] Where: "line" is the record number in each group of records on which the sort is to be done. Each group of records is delimited by a blank line. "line" defaults to 1. "file" is the file to be sorted. If no file is entered, stdin is used. Examples: msort -r 4 file msort -r3 file cat file | msort -r1 ' # # Handle command line options. # Sort on the first line by default. # SORTLINE=1 while : do case "$1" in --) shift; break ;; -r) SORTLINE="$2"; shift; shift ;; -r*) SORTLINE=`echo "$1" | sed 's/^..//'`; shift ;; -*) echo "$USAGE" >& 2; exit 1 ;; *) break ;; esac done # # Handle remaining arguments. # case "$#" in 0) INFILE="" ;; 1) INFILE="$1" ;; *) echo "$USAGE" >& 2; exit 1 ;; esac # # Convert the sort line to a field number, and process the data. # SORTFIELD=`expr $SORTLINE - 1` (cat $INFILE; echo) | sed -n ' 1 { /^$/d } : top N s/\n\(..*\)/@\1/g t top s/\n/@/p' | sort -t@ +$SORTFIELD | tr '@' '\012' exit 0