#!/bin/csh # MBARI 2003 # author: Kent Headley # bsort (batch sort) # Calls sort on a listed set of files. Originally used to # re-order OASIS files after downloads, to put recently recovered # records in the correct place in the file. The files are # sorted to a temporary directory, and then copied back # to the current working directory. The sorted file may # be copied to a new name (leaving the original untouched) # using the -F option. An alternative temporary directory may # be specified using -t; If the temporary directory doesn't # already exist, it is removed after processing is complete. # default temporary directory #set SORT_OPTIONS set TEMP=tmp # index into file list @ fp = 1 # list of files to process set FILE_LIST = () # list of new file names set NEW_FILE_LIST = () # Process command line args if ( $#argv == 0 ) then echo echo "sorts a list of files (optionally to a new file name)" echo "usage: $0:t [-t tempDir] [-p "\"""\""] [-F ]... " echo " F: output to new file name (default is )" echo " t: set temporary directory (creates ./tmp by default)" echo " p: pass options to sort" echo exit 0 endif @ k = 0 @ i = 1 while ( $#argv ) #echo arg is $argv[1] if ( ($argv[1] == "-p" ) ) then shift #echo "setting SORT_OPTIONS $argv[1]" set SORT_OPTIONS = $argv[1] else # if -T, override the default temp directory if ( ($argv[1] == "-t") ) then shift #echo "setting TEMP $argv[1]" set TEMP = $argv[1] else # if -F, override the default new file name if ( ($argv[1] == "-F" ) ) then shift @ j = $fp @ j-- set NEW_FILE_LIST[$j] = $argv[1] else # otherwise, it's a file name; give the new file # the same name by default set FILE_LIST = ($FILE_LIST[*] $argv[$i]) set NEW_FILE_LIST = ($NEW_FILE_LIST[*] $argv[$i]) @ fp++ endif endif endif endif shift end # set var to remove temp directory set REMOVE_TEMP = "NO" if ( !( -e $TEMP ) ) then echo creating $TEMP... mkdir $TEMP set REMOVE_TEMP = "YES" else echo $TEMP exists endif echo "TEMP $TEMP REMOVE=$REMOVE_TEMP" if ($?SORT_OPTIONS)then echo "SORT_OPTIONS $SORT_OPTIONS" endif echo "FILE_LIST $FILE_LIST" echo "NEW_FILE_LIST $NEW_FILE_LIST" # actually do the sorting... @ i = 1 while ($i <= $#FILE_LIST ) if (-e $FILE_LIST[$i]) then if ( $?SORT_OPTIONS ) then echo sorting $FILE_LIST[$i] to $TEMP/$NEW_FILE_LIST[$i]:t w/ options $SORT_OPTIONS sort "$SORT_OPTIONS" $FILE_LIST[$i] > $TEMP/$NEW_FILE_LIST[$i]:t else echo sorting $FILE_LIST[$i] to $TEMP/$NEW_FILE_LIST[$i]:t w/o options sort $FILE_LIST[$i] > $TEMP/$NEW_FILE_LIST[$i]:t endif mv $TEMP/$NEW_FILE_LIST[$i]:t $NEW_FILE_LIST[$i] endif @ i++ end # remove the temp directory if we should if ( $REMOVE_TEMP == "YES" ) then echo removing $TEMP... rmdir $TEMP endif echo done exit 0