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


Reply    
 
Thread Tools Search this Thread Display Modes
    #15  
Old Unix and Linux 5 Days Ago
SIMMS7400 SIMMS7400 is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 26 May 2017, 9:01 PM EDT
Posts: 91
Thanks: 19
Thanked 3 Times in 3 Posts
Hi Don -

Sorry for the confusion.

The shell I am using is bash.

I've updated how i calculated calendar quarter with your code:


Code:
_QUARTER=$(((month+2)/3))

As far as examples, here they are.

For Q1 of 2017:
FirstQtrWeek:12/31/16
LastQtrWeek: 03/25/17

For Q2 of 2017:
FirstQtrWeek:04/01/17
LastQtrWeek: 06/24/17

For Q3 of 2017:
FirstQtrWeek:07/01/17
LastQtrWeek: 09/23/17

For Q4 of 2017:
FirstQtrWeek:09/30/17
LastQtrWeek: 12/23/17

I'm sorry for the run around!

---------- Post updated at 05:23 PM ---------- Previous update was at 04:11 AM ----------

I've found out how to get the last Saturday of the quarter, however still having trouble getting the first Saturday:


Code:
GetFirstLastWeek () { 

DAY_OF_WEEK=6 # 1=Monday, ..., 7=Sunday
THE_YEAR=${_YEAR}
THE_MONTH="$1"

RESULT_DAY=0

YEAR_MONTH_STR=${THE_YEAR}"-"${THE_MONTH}  # Example value: "2017-06"

for DAYNO in 22 23 24 25 26 27 28 29 30 31
do
  DATE_TO_CHECK=${YEAR_MONTH_STR}"-"${DAYNO}
  DATE_RESULT=$(date --date=$DATE_TO_CHECK '+%u')
  if [ $? -eq 0 ]
  then
    if [ $DATE_RESULT -eq $DAY_OF_WEEK ]
    then
      RESULT_DAY=$DAYNO
    fi
  fi
done

RESULT_DATE_COMPLETE=${YEAR_MONTH_STR}"-"${RESULT_DAY}

echo $(date --date=$RESULT_DATE_COMPLETE +FirstQtrWeek,%m/%d/%Y -d "4 weeks ago")
echo $(date --date=$RESULT_DATE_COMPLETE +LastQtrWeek,%m/%d/%Y)

} > ${_SUBVARPATH}${_YEAR}_${_MONTH}${_DAY}/Subvar_List.txt

4 weeks ago from the LastQtrWeek doesn't work, obviously.
Sponsored Links
    #16  
Old Unix and Linux 5 Days Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 28 May 2017, 5:33 AM EDT
Location: Aachen, Germany
Posts: 10,827
Thanks: 272
Thanked 3,324 Times in 3,061 Posts
Why the scenic route? With GNU date, for the last Sunday in June (i.e. second quarter), try

Code:
date -d"2017-07-01 - $(date -d"2017-07-01" +%udays)"
So 25. Jun 00:00:00 CEST 2017

, and for the first Sat, try

Code:
date -d"2017-04-01 + $((6 - $(date -d2017-04-01 +%u)%7 )) days"
Sa 1. Apr 00:00:00 CEST 2017

Sponsored Links
    #17  
Old Unix and Linux 4 Days Ago
SIMMS7400 SIMMS7400 is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 26 May 2017, 9:01 PM EDT
Posts: 91
Thanks: 19
Thanked 3 Times in 3 Posts
Thank you, Rudi! I can't seem to get first saturday to work and I do have some concerns.

Also there is an exception which would fall on situation like September 30th, 2017. Even though it is still in 3rd uarter, it's technically the first quarter week of the 4th quarter and thus, the first Saturday. Therefore, October 7th would be the 2nd.

For instance, the first Saturday of the 4th quarter is still in September. (September 30th).

---------- Post updated at 12:53 PM ---------- Previous update was at 11:03 AM ----------

So the condition would be, unless that Saturday is the 1st, use the prior Saturday. As that would mean the first quarter week happens sometime between last sat and current Saturday - therefore use the previous sat.
    #18  
Old Unix and Linux 4 Days Ago
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 28 May 2017, 8:31 AM EDT
Location: San Jose, CA, USA
Posts: 10,308
Thanks: 521
Thanked 3,587 Times in 3,055 Posts
Quote:
Originally Posted by SIMMS7400 View Post
Thank you, Rudi! I can't seem to get first saturday to work and I do have some concerns.

Also there is an exception which would fall on situation like September 30th, 2017. Even though it is still in 3rd uarter, it's technically the first quarter week of the 4th quarter and thus, the first Saturday. Therefore, October 7th would be the 2nd.

For instance, the first Saturday of the 4th quarter is still in September. (September 30th).

---------- Post updated at 12:53 PM ---------- Previous update was at 11:03 AM ----------

So the condition would be, unless that Saturday is the 1st, use the prior Saturday. As that would mean the first quarter week happens sometime between last sat and current Saturday - therefore use the previous sat.
OK. So we're finally coming close to a definition of how to determine the 1st Saturday in a calendar quarter and, hopefully, the last Saturday in a calendar quarter.

You seem to have now confirmed that the 1st Saturday in a calendar quarter is the 1st Saturday before the 2nd day of the first full month of that quarter (as I asked in post #12 in this thread. And I presume (although you have never defined it) that the last Saturday of a calendar quarter is the Saturday before the 1st Saturday of the next calendar quarter. For CY 2017, this would mean that:

Code:
          Last Saturday of   1st Saturday of    Last Saturday of
Quarter   Previous Quarter   Current Quarter    Current Quarter
=======   ================   ===============    ================
1Q2017       12/24/2016        12/31/2016          03/25/2017
2Q2017       03/25/2017        04/01/2017          06/24/2017
3Q2017       06/24/2017        07/01/2017          09/23/2017
4Q2017       09/23/2017        09/30/2017          12/23/2017

Are all of these correct?
Sponsored Links
    #19  
Old Unix and Linux 4 Days Ago
SIMMS7400 SIMMS7400 is offline
Registered User
 
Join Date: Apr 2016
Last Activity: 26 May 2017, 9:01 PM EDT
Posts: 91
Thanks: 19
Thanked 3 Times in 3 Posts
Quote:
Originally Posted by Don Cragun View Post
OK. So we're finally coming close to a definition of how to determine the 1st Saturday in a calendar quarter and, hopefully, the last Saturday in a calendar quarter.

You seem to have now confirmed that the 1st Saturday in a calendar quarter is the 1st Saturday before the 2nd day of the first full month of that quarter (as I asked in post #12 in this thread. And I presume (although you have never defined it) that the last Saturday of a calendar quarter is the Saturday before the 1st Saturday of the next calendar quarter. For CY 2017, this would mean that:

Code:
          Last Saturday of   1st Saturday of    Last Saturday of
Quarter   Previous Quarter   Current Quarter    Current Quarter
=======   ================   ===============    ================
1Q2017       12/24/2016        12/31/2016          03/25/2017
2Q2017       03/25/2017        04/01/2017          06/24/2017
3Q2017       06/24/2017        07/01/2017          09/23/2017
4Q2017       09/23/2017        09/30/2017          12/23/2017

Are all of these correct?
Hi Don -

Thank you for the follow up. Yes those are correct- Thank you!!
Sponsored Links
Reply

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 04:23 PM
Parsing text file jacobs.smith Shell Programming and Scripting 7 08-15-2012 06:20 PM
Parsing text file comp8765 Shell Programming and Scripting 3 03-18-2012 05:39 PM
Column wise file parsing. navojit dutta Shell Programming and Scripting 5 02-02-2009 03:21 AM
Text File Parsing Djlethal Shell Programming and Scripting 2 02-27-2008 02:31 AM



All times are GMT -4. The time now is 12:36 PM.