------------------------------------------------------------------------
merge all files with same field IDs but with different filters
------------------------------------------------------------------------

Kevin Burdge has a directory with ZTF files:
	343_1, 343_2, 343_3
	456_1, 456_2
	823_1
The number to the left of the underscore is the field ID and that to
the right is the filter number (of which there are only 1, 2 and 3).
Kevin would like merge files with same field IDs.

$ ls *_[123] | sed 's/_[123]//g' |\
  sort -u | awk '{print "cat "$1 "_[123] > "$1}' 

cat 343_[123] > 343
cat 456_[123] > 456
cat 823_[123] > 823

Having satisfied that our script is producing the right command
sequence we now push for execution

$ ls *_[123] | sed 's/_[123]//g' |   \
  sort -u | awk '{print "cat "$1 "_[123] > "$1}' | sh -x
$ls 
343	343_1	343_2	343_3	456	456_1	456_2	456_3	823	823_1

Notes: I was careful to restrict the initial file search to files
ending with "_[123]". Onc could save four characters by using "_*".
Next, this script will "merge" files which have only one filter.
Either this is a feature or bug, depending on your perspective.


Now we can make a task out of this command.
------------------------------------------------------------------------
$ cat mfIDs

#!/bin/bash
# 
# mfIDs .. merge ZTF files "pppp_fid" with the same field ID (pppo)
#         and different filter IDs (fid=1,2,3).
#
# mfIDs    will display the commands to merge
# mfIDs -x will execute the commands. The merged files are "pppp"
#

if [ $# = 0 ]; then
        ls *_[123] | sed 's/_[123]//g' | sort -u |
        awk '{print "cat "$1 "_[123] > "$1}'
        exit
fi

if [ $# = 1 ]; then
   if [ $1 = "-x" ]; then
                ls *_[123] | sed 's/_[123]//g' | sort -u |
                awk '{print "cat "$1 "_[123] > "$1}' | sh -x
                exit
   else
      printf "%s  unknown option, exiting\n" $1; exit -1
   fi
else
   printf "%d  too many parameters; exiting\n" $#; exit -1
fi
------------------------------------------------------------------------