Unix/Linux Go Back    

UNIX for Beginners Questions & Answers If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

Parsing a column of text file - best practices

UNIX for Beginners Questions & Answers

Thread Tools Search this Thread Display Modes
Old Unix and Linux 09-26-2017   -   Original Discussion by SIMMS7400
SIMMS7400 SIMMS7400 is offline
Registered User
Join Date: Apr 2016
Last Activity: 17 November 2017, 7:24 AM EST
Posts: 143
Thanks: 35
Thanked 3 Times in 3 Posts
Hi Don -

Any update? Thank you! Linux
Sponsored Links
Old Unix and Linux 10-09-2017   -   Original Discussion by SIMMS7400
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Join Date: Jul 2012
Last Activity: 23 November 2017, 6:19 PM EST
Location: San Jose, CA, USA
Posts: 10,680
Thanks: 575
Thanked 3,738 Times in 3,190 Posts
I sincerely apologize for the lengthy delays. I had hoped to clean everything up with the log files, but I keep running into things where I just don't know what was wanted. So, here is the updated code that calculates and prints your variables. If you invoke your script with no arguments, it will print variables for the date on which the script was run if it is run on a Saturday and for the previous Saturday if it is not run on a Saturday. If you run it with one operand (a date in the format MM/DD/YYYY), and that date is a Saturday it will print variables for that date; if that date is not a Saturday, it will print variables for the Saturday prior to the given date.

Note that the date utility that I have on my system does not have a -d option and the bash on my system does not have ksh93's:

printf '%(format_string)T' "date_string"

date formatting capabilities. Therefore, the following code was tested using ksh93 instead of bash and a function named date was included in the script to translate the calls to date in this script to calls to ksh93's printf.

Hopefully, you can use the following as a basis for the changes you need to correctly calculate the date variables for your code. First, change _env.sh to:

#::-- Script Name: _env.sh						--::
#::--									--::
#::-- Description: This environment file is set to hold all variables	--::
#::--              for all Hyperion/Oracle scripts requiring:		--::
#::--									--::
#::--              1. Path variables                                        --::
#::--              2. User ID(s)                                            --::
#::--              3. Logon(s)                                              --::
#::--              4. Password(s)                                           --::
#::--              5. Server(s)                                             --::
#::--              6. Application(s)                                        --::
#::--              7. Database(s)                                          --::
#::--              8. etc                                                  --::
#::--									--::
#::--  Calls:      Not Applicable					--::
#::--  Called By:  All Scripts						--::
#::--									--::
#::-- Parameters:  Not Applicable					--::
#::--									--::
#::-- Author:      Name (Company )					--::
#::-- Date:								--::
#::									--::
#::-- Author:      Date vaiable handling updates by Donald W. Cragun    --::
#::-- Date:	   October 9, 2017					--::
#::									--::

#::-- Set Path Variables --::

cd $HOME


#::-- Set Essbase Variables --::



#::-- Set Time Variables --::

	$(date '+%d %m %Y %H %M %S')

	$(date -d "$_MONTH/$_DAY/$_YEAR 1 day ago" '+%d %m %Y')

#::-- Export Relevant Variables --::


and then change the first part of CS_Subvar_Advancement.sh to:

set -u			# abort on expansions of unset variables
IAm=${0##*/}		# basename of executing script

# Set path and Essbase variables... 
.  /home/oracle/Hyperion_Batch/Scripts/Batch/_env.sh

# findSaturdayOnOrBefore [date]
# This function finds the date that is the 1st date not later than the given
# date operand (in the format mm/dd/YYYY) or today's date if no date operand is
# given, and sets the following variables that apply to that date:
#	_Y	Full year.
#	_0y	Last two digits of year (00-99).
#	_m	Month number (1-12, no leading zero).
#	_0m	Two digit month number (01-12).
#	_d	Day of month (1-31, no leading zero).
#	_0d	Two digit day of month (01-31).
findSaturdayOnOrBefore() {
	# Break down the given date into its component parts.
	read _Y _0y _0m _m _0d _d __dow <<-EOF
		$(date -d "$__date" '+%Y %y %m %-m %d %-d %u')
	# date +%u output:  1=Monday, ..., 5=Friday, 6=Saturday, 7=Sunday
	# Is the given date a Saturday?
	if [ $__dow -ne 6 ]
	then	# No.  Adjust the given date to the previous Saturday.
		read _Y _0y _0m _m _0d _d <<-EOF
			$(date -d "$_0m/$_0d/$_Y $((__dow % 7 + 1)) days ago" \
			    '+%Y %y %m %-m %d %-d')

# The following can be simplified if this script will only be run on a Saturday
# in the week you want to process.  The code here allows this script to be run
# on any day of the week (in which case it will use the date of the previous
# Saturday if it isn't run on Saturday) or to specify a date as a command line
# operand (in MM/DD/YYYY format) to test processing of edge date conditions).
if [ $# -eq 0 ]
then	# Use today's date.
else	# Use command-line operand date if it appears to be a valid date.
	printf 'Date given as operand: "%s"\n' "$1"
	if date -d "$1" '+%m/%d/%Y' 2>/dev/null >&2
	then	findSaturdayOnOrBefore "$1"
	else	printf 'Usage: %s [date]\n\t%s\n' \
		    "$IAm" 'where "date" is of the form "MM/DD/YYYY"' >&2
		exit 2
printf 'Saturday at the start of the week is %s/%s/%s\n' $_0m $_0d $_Y

# Preserve values from this Saturday as Calendar Date values.

# Now that we have Saturday's Calendar date in the given week, determine the
# Fiscal year and month from the Calendar year and month of Friday at the end
# of this week.
read FY F0y F0m Fm F0d <<-EOF
	$(date -d "$C0m/$C0d/$CY + 6 days" '+%Y %y %m %-m %d')
printf 'Friday at the end of this week is %s/%s/%s\n' $F0m $F0d $FY

# Calculate fiscal quarter and half.
FQ=$(((Fm + 2) / 3))
FH=$(((FQ + 1) / 2))
printf 'Therefore, current week is in %sQ%s(%s) and %sH%s(%s)\n' \
    $FQ $FQY $FQ0y $FH $FHY $FH0y

# Calculate next fiscal quarter, previous two fiscal quarters, and previous two
# fiscal months..
FNQ=$(((FQ + 1) - 4 * (FQ == 4)))
FNQY=$((FQY + (FNQ < FQ)))
FNQ0y=$(printf '%02d' $((FNQY % 100)))

FPQ=$(((FQ - 1) + 4 * (FQ == 1)))
FPQY=$((FQY - (FQ < FPQ)))
FPQ0y=$(printf '%02d' $((FPQY % 100)))
FPPQ=$(((FPQ - 1) + 4 * (FPQ == 1)))
FPPQY=$((FQY - (FQ < FPPQ)))
FPPQ0y=$(printf '%02d' $((FPPQY % 100)))

FPm=$(((Fm - 1) + 12 * (Fm == 1)))
FPmY=$((FY - (Fm == 1)))
FPm0y=$(printf '%02d' $((FPmY % 100)))
FPPm=$(((FPm - 1) + 12 * (FPm == 1)))
FPPmY=$((FPmY - (FPm == 1)))
FPPm0y=$(printf '%02d' $((FPPmY % 100)))
printf 'FNQ=%sQ%s(%s)\n' $FNQ $FNQY $FNQ0y
printf 'FPQ=%sQ%s(%s), FPPQ=%sQ%s(%s)\n' $FPQ $FPQY $FPQ0y $FPPQ $FPPQY $FPPQ0y
printf 'Fiscal Month: %s%s(%s)\n' ${months[$Fm]} $FY $F0y
printf 'Previous Fiscal Month: %s%s(%s)\n' ${months[$FPm]} $FPmY $FPm0y
printf '2nd Previous Fiscal Month: %s%s(%s)\n' ${months[$FPPm]} $FPPmY $FPPm0y

# Find the 1st Saturday in this quarter.
findSaturdayOnOrBefore $(printf '%02d' $((FQ * 3 - 2)))/01/$FQY
printf 'Calculated 1st Saturday of %sQ%s(%s): %s/%s/%s(%s/%s/%s)\n' \
    $FQ $FQY $FQ0y $FQ1stSat0m $FQ1stSat0d $FQ1stSatY \
    $FQ1stSatm $FQ1stSatd $FQ1stSat0y

# Find the last Saturday in this quarter.  Start by finding the 1st Saturday in
# the next quarter and then find the Saturday before that.
findSaturdayOnOrBefore $(printf '%02d' $((FNQ * 3 - 2)))/01/$FNQY
printf 'Calculated 1st Saturday of %sQ%s(%s): %s/%s/%s(%s/%s/%s)\n' \
    $FNQ $FNQY $FNQ0y $_0m $_0d $_Y $_m $_d $_0y
read FQlastSatY FQlastSat0y FQlastSat0m FQlastSatm FQlastSat0d FQlastSatd <<-EOF
	$(date -d "$_0m/$_0d/$_Y 7 days ago" '+%Y %y %m %-m %d %-d')
printf 'Calculated last Saturday of %sQ%s(%s): %s/%s/%s(%s/%s/%s)\n' \
    $FQ $FQY $FQ0y $FQlastSat0m $FQlastSat0d $FQlastSatY \
    $FQlastSatm $FQlastSatd $FQlastSat0y

printf '\nCalculations are done & Variables are set, print results:\n\n'

printf '"1PeriodPrior",%s%s\n' ${months[$FPm]} $FPm0y
printf "\"1PeriodPriorq\",'\"%s%s\"'\n" ${months[$FPm]} $FPm0y
printf '"2PeriodPrior",%s%s\n' ${months[$FPPm]} $FPPm0y
printf "\"2PeriodPriorq\",'\"%s%s\"'\n" ${months[$FPPm]} $FPPm0y
printf 'ALLC_CurrentPeriod,%s%s\n' ${months[$F0m]} $F0y
printf "ALLC_CurrentPeriodq,'\"%s%s\"'\n" ${months[$F0m]} $F0y
printf "ALLC_CurrentWeek,'%s/%s/%s'\n" $C0m $C0d $C0y
printf "ALLC_CurrentWeekq,'\"%s/%s/%s\"'\n" $C0m $C0d $C0y
#printf "CurrentHalf,'FY %sH%s'\n" $FH $FHY  # Not requested but seems strange.
printf "CurrentHalfq,'\"FY %sH%s\"'\n" $FH $FHY
printf 'CurrentPeriod,%s%s\n' ${months[$F0m]} $F0y
printf "CurrentPeriodq,'\"%s%s\"'\n" ${months[$F0m]} $F0y
printf "CurrentPlanYear,'FY %s'\n" $FQY
printf "CurrentPlanYearq,'\"FY %s\"'\n" $FQY
printf "CurrentQtrInput,'FY %sQ%s_input'\n" $FQ $FQY
printf "CurrentQtrInputq,'\"FY %sQ%s_input\"'\n" $FQ $FQY
printf "CurrentQuarter,'FY %sQ%s'\n" $FQ $FQY
printf "CurrentQuarterq,'\"FY %sQ%s\"'\n" $FQ $FQY
printf "CurrentWeek,'%s/%s/%s'\n" $C0m $C0d $C0y
printf "CurrentWeekq,'\"%s/%s/%s\"'\n" $C0m $C0d $C0y
printf "CurrentYear,'FY %s'\n" $FQY
printf "CurrentYearq,'\"FY %s\"'\n" $FQY
printf "FirstQtrWeek,'%s/%s/%s'\n" $FQ1stSat0m $FQ1stSat0d $FQ1stSatY
printf "LastQtrWeek,'%s/%s/%s'\n" $FQlastSat0m $FQlastSat0d $FQlastSatY
printf "PriorQtrInput,'FY %sQ%s_input'\n" $FPQ $FPQY
printf "PriorQuarter,'FY %sQ%s'\n" $FPQ $FPQY
printf "PriorQuarterq,'\"FY %sQ%s\"'\n" $FPQ $FPQY
printf "PriorQuarterAD,'AD%s-%s'\n" $FPQ $FPQ0y
printf "PriorQuarterADq,'\"AD%s-%s\"'\n" $FPQ $FPQ0y
printf 'PriorQuarterMnth1,%s%s\n' ${months[$(($FPQ * 3 - 2))]} $FPQ0y
printf "PriorQuarterMnth1q,'\"%s%s\"'\n" ${months[$(($FPQ * 3 - 2))]} $FPQ0y
printf 'PriorQuarterMnth2,%s%s\n' ${months[$(($FPQ * 3 - 1))]} $FPQ0y
printf "PriorQuarterMnth2q,'\"%s%s\"'\n" ${months[$(($FPQ * 3 - 1))]} $FPQ0y
printf 'PriorQuarterMnth3,%s%s\n' ${months[$(($FPQ * 3))]} $FPQ0y
printf "PriorQuarterMnth3q,'\"%s%s\"'\n" ${months[$(($FPQ * 3))]} $FPQ0y

Note that I was using the set -u at the start of this script to find and set values for variables you were using that had not been set. You might (or might not) want to remove that line.

Note also that the other lines shown in red in the above script can be removed without affecting the variables set by this script. But, they were very useful while debugging earlier attempts at getting the results you wanted.
The Following User Says Thank You to Don Cragun For This Useful Post:
Neo (10-10-2017)
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Parsing a fixed column text file in sed wvdeijk Shell Programming and Scripting 4 06-25-2015 05:23 PM
Parsing text file jacobs.smith Shell Programming and Scripting 7 08-15-2012 07:20 PM
Parsing text file comp8765 Shell Programming and Scripting 3 03-18-2012 06:39 PM
Column wise file parsing. navojit dutta Shell Programming and Scripting 5 02-02-2009 04:21 AM
Text File Parsing Djlethal Shell Programming and Scripting 2 02-27-2008 03:31 AM

All times are GMT -4. The time now is 11:54 PM.