#!/bin/csh -f # union # ----------------------------------------------------------------------------- # C shell script to show the union of the lines in the 2 specified files, # echoing a sorted list of lines that occur in one or both files. # See also: subtract, intersect, join # ----------------------------------------------------------------------------- # Usage: See Usage section below or run with -h or --help to see usage. # Assumptions: # Effects: # Notes: # Implementation Notes: # Portability Issues: # Revision History: # $Log$ # ----------------------------------------------------------------------------- # Collect command line options set option_ignore_case = "false" set option_all = "false" while ($#argv > 0) if ("$1:q" == "-h" || "$1:q" == "--help") then echo "Usage: $0:t [options] file1 file2" echo " Use - as a filename to read from standard input" echo "Options:" echo " -h = Show this help info" echo " --help = Show this help info" echo " -i = Ignore case when comparing lines" echo " -a = All lines, including duplicates, all that occur in " echo " one file plus all that occur in the other file" exit 1 else if ("$1:q" == "-i") then shift set option_ignore_case = "true" else if ("$1:q" == "-a") then shift set option_all = "true" else if ("$1" != "-" && "-" == "`echo $1:q | cut -c 1`") then echo "Error: Invalid option: $1:q" $0 --help exit 1 else # Not a recognized option. Assume it's the first parameter break endif end set sort_option_ignore_case_string = "" if ("$option_ignore_case" == "true") then set sort_option_ignore_case_string = "--ignore-case" endif set sort_option_unique_string = "" if ("$option_all" == "false") then set sort_option_unique_string = "--unique" endif # Collect command line arguments if ($#argv > 2) then echo "Error: Too many arguments" $0:t --help exit 1 else if ($#argv < 1) then echo "Error: file1 and file2 are required" $0:t --help exit 1 else if ($#argv < 2) then echo "Error: file2 is required" $0:t --help exit 1 else if ("$1" != "-" && ! -e $1:q) then echo "Error: File $1:q does not exist." exit 1 else if ("$1" != "-" && ! -r $1:q) then echo "Error: File $1:q is not readable." exit 1 else if ("$2" != "-" && ! -e $2:q) then echo "Error: File $2:q does not exist." exit 1 else if ("$2" != "-" && ! -r $2:q) then echo "Error: File $2:q is not readable." exit 1 endif set timestamp = `date "+%Y_%m_%d__%H_%M_%S"` set tempfile1 = "/tmp/$0:t_$$_file1_$timestamp" set tempfile2 = "/tmp/$0:t_$$_file2_$timestamp" sort $sort_option_ignore_case_string $sort_option_unique_string $1:q > $tempfile1:q sort $sort_option_ignore_case_string $sort_option_unique_string $2:q > $tempfile2:q sort $sort_option_ignore_case_string $sort_option_unique_string --merge $tempfile1:q $tempfile2:q rm $tempfile1:q rm $tempfile2:q