#! /bin/bash # extract specified columns from hydrobal.out. Columns are # are enclosed in |. Excess columns are removed one at a time. # columns 1, 2, 3, 7, and 11 cannot be retained #echo "$*" # check for input file name existence if [[ $1 = "" ]] then echo 'invocation: getcols [file-1 file-2] [Col-1 Col-2 ... Col-n]' echo 'File names are required' exit fi infile="$1" shift # check for output file name existence if [[ $1 = "" ]] then echo 'invocation: getcols [file-1 file-2] [Col-1 Col-2 ... Col-n]' echo 'File names are required' exit fi outfile="$1" shift # invocation: getcols [file-1 file-2] [Col-1 Col-2 ... Col-n] # set up column and title correspondence mincol=4 maxcol=18 coltitle[4]=`echo "4 - start day of hydrobalance period"` coltitle[5]=`echo "5 - starting soil water content (mm)"` coltitle[6]=`echo "6 - starting snow water content (mm)"` coltitle[7]=`echo "7 - DO NOT USE THIS NUMBER text field (auto delete)"` coltitle[8]=`echo "8 - end day of hydrobalance period"` coltitle[9]=`echo "9 - ending soil water content (mm)"` coltitle[10]=`echo "10 - ending snow water content (mm)"` coltitle[11]=`echo "11 - DO NOT USE THIS NUMBER text field (auto delete)"` coltitle[12]=`echo "12 - rainfall for period (mm)"` coltitle[13]=`echo "13 - runoff for period (mm)"` coltitle[14]=`echo "14 - evaporation for period (mm)"` coltitle[15]=`echo "15 - transpiration for period (mm)"` coltitle[16]=`echo "16 - drainage for period (mm)"` coltitle[17]=`echo "17 - check for period (mm)"` coltitle[18]=`echo "18 - fallow efficiency (delta swc / precip)"` if [[ $1 = "" ]] then echo 'invocation: getcols [file-1 file-2] [Col-1 Col-2 ... Col-n]' echo 'Valid Column Numbers are:' for num in $( seq $mincol $maxcol ) do echo ${coltitle[num]} done exit fi # make sure column numbers are within range # set up array of selected column numbers. Values in array index 0 to maxcolnum-1 maxcolnum=0 for colnum in $* do if [[ ${colnum} -lt $mincol || ${colnum} -gt $maxcol || ${colnum} -eq 7 || ${colnum} -eq 11 ]] then echo 'invocation: getcols [file-1 file-2] [Col-1 Col-2 ... Col-n]' echo 'Valid Column Numbers are:' for num in $( seq $mincol $maxcol ) do echo ${coltitle[num]} done exit fi selcolnum[maxcolnum]=$colnum let maxcolnum=maxcolnum+1 done selcolnum[maxcolnum]=0 # make sure column numbers are in ascending order # if they are not in order, the larger number will not allow retaining the smaller numbers that follow # remove initial date field column sed s/[^\|.]*\|/\|/ "${infile}" > "${outfile}" cp "${outfile}" atemp #echo "atemp updated - First column" #read # with column 1 removed, set colnum to 2 colnum=2 selindex=0 blocknum=1 while [ `grep -c \| atemp` -gt 0 ] do if [[ ${colnum} -eq 1 || ${colnum} -eq 2 || ${colnum} -eq 3 || ${colnum} -eq 7 || ${colnum} -eq 11 ]] then # automatically deleted columns sed s/\|[^\|]*\|/\|/ atemp > "${outfile}" # remove autodelete column #echo "${outfile} updated - autodelete column ${colnum}" #read let colnum=colnum+1 cp "${outfile}" atemp fi # selected columns #echo "checking column $colnum to retain column ${selcolnum[selindex]} at selindex $selindex" if [[ ${colnum} -eq ${selcolnum[selindex]} ]] then sed s/\|/\ / atemp > "${outfile}" # remove extra column separator let selindex=selindex+1 #echo "${outfile} updated - column ${colnum} retained" #read else # count number of delimiters left in first line delimcnt=`sed -n '1p' < atemp | tr -cd \| | wc -c` #echo "delimcnt = $delimcnt" if [ $delimcnt -gt 1 ] then # remove column between | characters sed s/\|[^\|]*\|/\|/ atemp > "${outfile}" else # remove last column sed s/\|[^\|]*$// atemp > "${outfile}" fi #echo "${outfile} updated - column ${colnum} deleted" #read fi let colnum=colnum+1 cp "${outfile}" atemp # reset counters to get next column group if [[ ${colnum} -gt ${maxcol} ]] then colnum=1 selindex=0 #echo "Block $blocknum Completed" let blocknum=blocknum+1 fi done rm atemp