fisrt date of the quarter
fisrt date of the quarter
am 18.09.2007 23:06:16 von rave25
need a one line command to do something like this :
take date in format YYYYMMDD and return the first date of that
quarter.
suppose DATE=20070918; the command should retun date=20070701
suppose DATE=20070328; the command should return date=20070101
suppose DATE=20070511; the command should return date=20070401
suppose DATE=20071018; the command should return date=20071001
suppose DATE=20071125; the command should return date=20071001
Thanks
Re: fisrt date of the quarter
am 19.09.2007 03:04:57 von Bill Marcum
On Tue, 18 Sep 2007 21:06:16 -0000, romy
wrote:
>
>
> need a one line command to do something like this :
>
> take date in format YYYYMMDD and return the first date of that
> quarter.
> suppose DATE=20070918; the command should retun date=20070701
> suppose DATE=20070328; the command should return date=20070101
> suppose DATE=20070511; the command should return date=20070401
> suppose DATE=20071018; the command should return date=20071001
> suppose DATE=20071125; the command should return date=20071001
>
> Thanks
>
You could write this on one line, but it would be a long line.
year=${DATE%????}
case ${${DATE#????}%??} in
0[1-3]) echo ${year}0101 ;;
0[4-6]) echo ${year}0401 ;;
0[7-9]) echo ${year}0701 ;;
1[0-2]) echo ${year}1001 ;;
*) echo error! ;;
esac
--
Rotten wood cannot be carved.
-- Confucius, "Analects", Book 5, Ch. 9
Re: fisrt date of the quarter
am 19.09.2007 04:21:34 von William James
On Sep 18, 4:06 pm, romy wrote:
> need a one line command to do something like this :
>
> take date in format YYYYMMDD and return the first date of that
> quarter.
> suppose DATE=20070918; the command should retun date=20070701
> suppose DATE=20070328; the command should return date=20070101
> suppose DATE=20070511; the command should return date=20070401
> suppose DATE=20071018; the command should return date=20071001
> suppose DATE=20071125; the command should return date=20071001
>
> Thanks
ruby -ne'$_=~/([^0]?.)(..\n)/;print $`,($1.to_i-1)/3*3+1,$2'
awk '{$6=int(($5$6-1)/3)*3+1;if($5)$5="";print}' OFS="" FS=""
Re: fisrt date of the quarter
am 19.09.2007 05:01:30 von Ed Morton
William James wrote:
> On Sep 18, 4:06 pm, romy wrote:
>
>>need a one line command to do something like this :
>>
>>take date in format YYYYMMDD and return the first date of that
>>quarter.
>>suppose DATE=20070918; the command should retun date=20070701
>>suppose DATE=20070328; the command should return date=20070101
>>suppose DATE=20070511; the command should return date=20070401
>>suppose DATE=20071018; the command should return date=20071001
>>suppose DATE=20071125; the command should return date=20071001
>>
>>Thanks
>
> awk '{$6=int(($5$6-1)/3)*3+1;if($5)$5="";print}' OFS="" FS=""
>
Nice approach, but not quite:
$ echo 20071018 | awk '{$6=int(($5$6-1)/3)*3+1;if($5)$5="";print}'
OFS="" FS=""
2007118
This should do it:
$ echo 20071018 | awk '{printf
"%s%02d01\n",$1$2$3$4,int((($5$6)-1)/3)*3+1}' FS=""
20071001
Regards,
Ed.
Re: fisrt date of the quarter
am 19.09.2007 05:50:18 von cfajohnson
On 2007-09-19, Bill Marcum wrote:
> On Tue, 18 Sep 2007 21:06:16 -0000, romy
> wrote:
>>
>>
>> need a one line command to do something like this :
>>
>> take date in format YYYYMMDD and return the first date of that
>> quarter.
>> suppose DATE=20070918; the command should retun date=20070701
>> suppose DATE=20070328; the command should return date=20070101
>> suppose DATE=20070511; the command should return date=20070401
>> suppose DATE=20071018; the command should return date=20071001
>> suppose DATE=20071125; the command should return date=20071001
>>
>> Thanks
>>
> You could write this on one line, but it would be a long line.
> year=${DATE%????}
> case ${${DATE#????}%??} in
xx.sh: line 3: ${${DATE#????}%??}: bad substitution
You cannot extract the middle of a string with a single parameter
expansion (in a POSIX script). Use:
md=${DATE#????}
case ${md%??} in
> 0[1-3]) echo ${year}0101 ;;
> 0[4-6]) echo ${year}0401 ;;
> 0[7-9]) echo ${year}0701 ;;
> 1[0-2]) echo ${year}1001 ;;
> *) echo error! ;;
> esac
--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Re: fisrt date of the quarter
am 19.09.2007 15:00:43 von rave25
On Sep 18, 9:04 pm, Bill Marcum wrote:
> On Tue, 18 Sep 2007 21:06:16 -0000, romy wrote:
>
> > need a one line command to do something like this :
>
> > take date in format YYYYMMDD and return the first date of that
> > quarter.
> > suppose DATE=20070918; the command should retun date=20070701
> > suppose DATE=20070328; the command should return date=20070101
> > suppose DATE=20070511; the command should return date=20070401
> > suppose DATE=20071018; the command should return date=20071001
> > suppose DATE=20071125; the command should return date=20071001
>
> > Thanks
>
> You could write this on one line, but it would be a long line.
> year=${DATE%????}
> case ${${DATE#????}%??} in
> 0[1-3]) echo ${year}0101 ;;
> 0[4-6]) echo ${year}0401 ;;
> 0[7-9]) echo ${year}0701 ;;
> 1[0-2]) echo ${year}1001 ;;
> *) echo error! ;;
> esac
>
> --
> Rotten wood cannot be carved.
> -- Confucius, "Analects", Book 5, Ch. 9
thanks, but i am looking for a more sleek solution..something just in
one line.
Re: fisrt date of the quarter
am 19.09.2007 15:27:20 von Janis Papanagnou
On 19 Sep., 15:00, romy wrote:
> On Sep 18, 9:04 pm, Bill Marcum wrote:
>
>
>
> > On Tue, 18 Sep 2007 21:06:16 -0000, romy wrote:
>
> > > need a one line command to do something like this :
>
> > > take date in format YYYYMMDD and return the first date of that
> > > quarter.
> > > suppose DATE=20070918; the command should retun date=20070701
> > > suppose DATE=20070328; the command should return date=20070101
> > > suppose DATE=20070511; the command should return date=20070401
> > > suppose DATE=20071018; the command should return date=20071001
> > > suppose DATE=20071125; the command should return date=20071001
>
> > > Thanks
>
> > You could write this on one line, but it would be a long line.
> > year=${DATE%????}
> > case ${${DATE#????}%??} in
> > 0[1-3]) echo ${year}0101 ;;
> > 0[4-6]) echo ${year}0401 ;;
> > 0[7-9]) echo ${year}0701 ;;
> > 1[0-2]) echo ${year}1001 ;;
> > *) echo error! ;;
> > esac
>
> > --
> > Rotten wood cannot be carved.
> > -- Confucius, "Analects", Book 5, Ch. 9
>
> thanks, but i am looking for a more sleek solution..something just in
> one line.
Put it in a function and call it in one line.
Janis
Re: fisrt date of the quarter
am 19.09.2007 17:02:35 von William James
On Sep 18, 4:06 pm, romy wrote:
> need a one line command to do something like this :
>
> take date in format YYYYMMDD and return the first date of that
> quarter.
> suppose DATE=20070918; the command should retun date=20070701
> suppose DATE=20070328; the command should return date=20070101
> suppose DATE=20070511; the command should return date=20070401
> suppose DATE=20071018; the command should return date=20071001
> suppose DATE=20071125; the command should return date=20071001
>
> Thanks
I still can't see my post on google, so here it goes again.
awk '{$6=int(($5$6-1)/3)*3+1;if($5)$5=""}1' FS="" OFS=""
Re: fisrt date of the quarter
am 19.09.2007 21:58:39 von Cyrus Kriticos
romy wrote:
> On Sep 18, 9:04 pm, Bill Marcum wrote:
>> On Tue, 18 Sep 2007 21:06:16 -0000, romy wrote:
>>
>>> need a one line command to do something like this :
>>> take date in format YYYYMMDD and return the first date of that
>>> quarter.
>>> suppose DATE=20070918; the command should retun date=20070701
>>> suppose DATE=20070328; the command should return date=20070101
>>> suppose DATE=20070511; the command should return date=20070401
>>> suppose DATE=20071018; the command should return date=20071001
>>> suppose DATE=20071125; the command should return date=20071001
>>> Thanks
>> You could write this on one line, but it would be a long line.
>> year=${DATE%????}
>> case ${${DATE#????}%??} in
>> 0[1-3]) echo ${year}0101 ;;
>> 0[4-6]) echo ${year}0401 ;;
>> 0[7-9]) echo ${year}0701 ;;
>> 1[0-2]) echo ${year}1001 ;;
>> *) echo error! ;;
>> esac
>>
>
> thanks, but i am looking for a more sleek solution..something just in
> one line.
Using Ed Morton's formula with bash version > 3.0:
$ X=20070511
$ printf -v X "%d%.2d01\n" ${X:0:4} $(((${X:4:2}-1)/3*3+1))
$ echo $X
20070401
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: fisrt date of the quarter
am 19.09.2007 22:32:44 von Cyrus Kriticos
romy wrote:
> On Sep 18, 9:04 pm, Bill Marcum wrote:
>> On Tue, 18 Sep 2007 21:06:16 -0000, romy wrote:
>>
>>> need a one line command to do something like this :
>>> take date in format YYYYMMDD and return the first date of that
>>> quarter.
>>> suppose DATE=20070918; the command should retun date=20070701
>>> suppose DATE=20070328; the command should return date=20070101
>>> suppose DATE=20070511; the command should return date=20070401
>>> suppose DATE=20071018; the command should return date=20071001
>>> suppose DATE=20071125; the command should return date=20071001
>>> Thanks
>> You could write this on one line, but it would be a long line.
>> year=${DATE%????}
>> case ${${DATE#????}%??} in
>> 0[1-3]) echo ${year}0101 ;;
>> 0[4-6]) echo ${year}0401 ;;
>> 0[7-9]) echo ${year}0701 ;;
>> 1[0-2]) echo ${year}1001 ;;
>> *) echo error! ;;
>> esac
>>
>
> thanks, but i am looking for a more sleek solution..something just in
> one line.
Using Ed Morton's formula with bash version > 3.0:
$ X=20070511
$ printf -v X "%d%.2d%s" ${X:0:4} $(((${X:4:2}-1)/3*3+1)) 01
$ echo $X
20070401
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: fisrt date of the quarter
am 19.09.2007 22:46:51 von Cyrus Kriticos
Bill Marcum wrote:
> year=${DATE%????}
> case ${${DATE#????}%??} in
> 0[1-3]) echo ${year}0101 ;;
> 0[4-6]) echo ${year}0401 ;;
> 0[7-9]) echo ${year}0701 ;;
> 1[0-2]) echo ${year}1001 ;;
> *) echo error! ;;
> esac
or
case ${DATE:4:2} in
0[1-3]) echo ${DATE:0:5}101 ;;
0[4-6]) echo ${DATE:0:5}401 ;;
0[7-9]) echo ${DATE:0:5}701 ;;
1[0-2]) echo ${DATE:0:5}001 ;;
esac
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: fisrt date of the quarter
am 19.09.2007 23:53:12 von cfajohnson
On 2007-09-19, Cyrus Kriticos wrote:
> Bill Marcum wrote:
>
>> year=${DATE%????}
>> case ${${DATE#????}%??} in
>> 0[1-3]) echo ${year}0101 ;;
>> 0[4-6]) echo ${year}0401 ;;
>> 0[7-9]) echo ${year}0701 ;;
>> 1[0-2]) echo ${year}1001 ;;
>> *) echo error! ;;
>> esac
>
> or
>
> case ${DATE:4:2} in
That parameter expansion is not part of the POSIX standard.
> 0[1-3]) echo ${DATE:0:5}101 ;;
> 0[4-6]) echo ${DATE:0:5}401 ;;
> 0[7-9]) echo ${DATE:0:5}701 ;;
> 1[0-2]) echo ${DATE:0:5}001 ;;
> esac
--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Re: fisrt date of the quarter
am 20.09.2007 01:40:25 von Michal Nazarewicz
romy writes:
> need a one line command to do something like this :
>
> take date in format YYYYMMDD and return the first date of that
> quarter.
> suppose DATE=20070918; the command should retun date=20070701
> suppose DATE=20070328; the command should return date=20070101
> suppose DATE=20070511; the command should return date=20070401
> suppose DATE=20071018; the command should return date=20071001
> suppose DATE=20071125; the command should return date=20071001
printf %s%02d01 ${DATE%????} $(((${DATE%??}%100-1)/3*3+1))
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +-------ooO--(_)--Ooo--
Re: fisrt date of the quarter
am 20.09.2007 01:43:00 von Michal Nazarewicz
romy writes:
> need a one line command to do something like this :
>
> take date in format YYYYMMDD and return the first date of that
> quarter.
> suppose DATE=20070918; the command should retun date=20070701
> suppose DATE=20070328; the command should return date=20070101
> suppose DATE=20070511; the command should return date=20070401
> suppose DATE=20071018; the command should return date=20071001
> suppose DATE=20071125; the command should return date=20071001
printf ${DATE%????}%0201 $(((${DATE%??}%100-1)/3*3+1))
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +-------ooO--(_)--Ooo--
Re: fisrt date of the quarter
am 20.09.2007 02:24:35 von Tiago Peczenyj
try this:
date -d "${DATE}" +'%Y%m:01' | sed 's/1[12]:/10/;s/[23]:/1/;s/[56]:/
4/;s/[89]:/7/;s/://'
On 19 set, 20:40, Michal Nazarewicz wrote:
> romy writes:
> > need a one line command to do something like this :
>
> > take date in format YYYYMMDD and return the first date of that
> > quarter.
> > suppose DATE=20070918; the command should retun date=20070701
> > suppose DATE=20070328; the command should return date=20070101
> > suppose DATE=20070511; the command should return date=20070401
> > suppose DATE=20071018; the command should return date=20071001
> > suppose DATE=20071125; the command should return date=20071001
>
> printf %s%02d01 ${DATE%????} $(((${DATE%??}%100-1)/3*3+1))
>
> --
> Best regards, _ _
> .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
> ..o | Computer Science, Michal "mina86" Nazarewicz (o o)
> ooo +-------ooO--(_)--Ooo--
Re: fisrt date of the quarter
am 20.09.2007 03:42:14 von Michal Nazarewicz
Tiago Peczenyj writes:
Do not top post.
>> romy writes:
>>> need a one line command to do something like this :
>>
>>> take date in format YYYYMMDD and return the first date of that
>>> quarter.
>>> suppose DATE=20070918; the command should retun date=20070701
>>> suppose DATE=20070328; the command should return date=20070101
> date -d "${DATE}" +'%Y%m:01' | sed 's/1[12]:/10/;s/[23]:/1/;s/[56]:/
> 4/;s/[89]:/7/;s/://'
O_o It could use some optimisation:
echo "${DATE%??}:01" | sed 's/1.:/10/;s/[123]:/1/;s/[456]:/4/;s/.:/7/'
I don't believe this is POSIX compatible though. A POSIX way would be
to replace semicolons with new lines and that would break the one liner
requirement.
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +-------ooO--(_)--Ooo--
Re: fisrt date of the quarter
am 20.09.2007 15:19:44 von Geoff Clare
Michal Nazarewicz wrote:
> echo "${DATE%??}:01" | sed 's/1.:/10/;s/[123]:/1/;s/[456]:/4/;s/.:/7/'
>
> I don't believe this is POSIX compatible though.
It is fine by POSIX. The sed commands that POSIX says can't be
followed by a semicolon are {, a, b, c, i, r, t, w, :, and #.
You also can't use a semicolon to terminate an s command that has
a w flag (because the semicolon could be taken as part of the
filename to write).
> A POSIX way would be
> to replace semicolons with new lines and that would break the one liner
> requirement.
Even if you couldn't use the semicolons, it can still be done as a
one liner by using multiple -e options.
--
Geoff Clare
Re: fisrt date of the quarter
am 21.09.2007 23:58:16 von rave25
On Sep 20, 9:19 am, Geoff Clare
wrote:
> Michal Nazarewicz wrote:
> > echo "${DATE%??}:01" | sed 's/1.:/10/;s/[123]:/1/;s/[456]:/4/;s/.:/7/'
>
> > I don't believe this is POSIX compatible though.
>
> It is fine by POSIX. The sed commands that POSIX says can't be
> followed by a semicolon are {, a, b, c, i, r, t, w, :, and #.
> You also can't use a semicolon to terminate an s command that has
> a w flag (because the semicolon could be taken as part of the
> filename to write).
>
> > A POSIX way would be
> > to replace semicolons with new lines and that would break the one liner
> > requirement.
>
> Even if you couldn't use the semicolons, it can still be done as a
> one liner by using multiple -e options.
>
> --
> Geoff Clare
Thank you Guys, you all are Super !!