(OLD) (ARCHIVED) Puppy Linux Discussion Forum Forum Index (OLD) (ARCHIVED) Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info

This forum can also be accessed as http://oldforum.puppylinux.com
It is now read-only and serves only as archives.

Please register over the NEW forum
https://forum.puppylinux.com
and continue your work there. Thank you.

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups    
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Tue 09 Mar 2021, 05:25
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
"This Week" script: in bash, awk, or whatever.
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies. View previous topic :: View next topic
Page 2 of 7 [97 Posts]   Goto page: Previous 1, 2, 3, 4, 5, 6, 7 Next
Author Message
musher0

Joined: 04 Jan 2009
Posts: 15041
Location: Gatineau (Qc), Canada

PostPosted: Mon 19 Nov 2018, 18:36    Post subject:  

Sh!t, puppy_apprentice, that's too good! Very Happy

~~~~~~~~~
Except for my locale,
Code:
date=$(date);echo ${date:4:3}
gives me
Quote:
i 1
Better to use
Code:
date=$(date '+%d')
, I think, since < date > is already aware of the locale.

With my edits:
Code:
#!/bin/sh
# /root/my-applications/bin/cur-week.sh
# par / by puppy-apprentice, 19 nov. 2018
####
cal | head -n 2
date=$(date '+%d') # edited
case "${LANG:0:2}" in # added
   fr)Wk="semaine" ;;
#   your_language_here)Wk="..." #
   *)Wk="week" ;;
esac # end of edit
 line=$(cal | grep -n ${date})
 echo $(cal | grep --color=always ${date}) \($Wk $((${line:0:1}-2))\)

But your thought of using < cal | grep -n ${date} > is brilliant! Smile

Please see attached result in my language below.

BFN.
cur-week.jpg
 Description   
 Filesize   29.68 KB
 Viewed   242 Time(s)

cur-week.jpg


_________________
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

Last edited by musher0 on Mon 19 Nov 2018, 19:35; edited 4 times in total
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 15041
Location: Gatineau (Qc), Canada

PostPosted: Mon 19 Nov 2018, 18:40    Post subject:  

nosystemdthanks wrote:
musher0 wrote:
@nosystemdthanks

the fact that you are still amused shows both a sophisticated sense of humour, as well as a saint-like level of patience. i should congratulate you on both.

From now on, I grant you permission to address me as "St. Musher0". Laughing

_________________
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)
Back to top
View user's profile Send private message 
step

Joined: 04 May 2012
Posts: 1352

PostPosted: Mon 19 Nov 2018, 19:02    Post subject:  

@nosystemdthanks, thanks for the links. You didn't say you're the author of fig, I searched the board and found the thread about compiling Basic and all that, where you describe some of your design philosophy. Back to fig, when I read your program above for a moment I thought that fig was stack based, like forth, but it had too many words to be forth. I think it's the implicitness that fig allows that makes it resemble a stack based language. But then I did recognize the Basic in it, and python, and that got me curious. Thanks for sharing.
_________________
Fatdog64-810|+Packages|Kodi|gtkmenuplus
Back to top
View user's profile Send private message 
nosystemdthanks


Joined: 03 May 2018
Posts: 724

PostPosted: Mon 19 Nov 2018, 19:29    Post subject:  

step, cheers.

its not stack-based, no. it was originally called "fig basic" (no relation to fig forth-- nor to docker fig, which is now docker compose.) if at some point in the future you have other questions, feel free.

musher0 wrote:
From now on, I grant you permission to address me as "St. Musher0". :lol:


and if that doesnt confuse postes canada, nothing will.

"another one for st. musher0, eh." "is that a street number or part of the name?" "i think its part of the name, like 'mØ'. though they put 'street' first, so its anybodys guess!" "wait, look at this street sign-- 'st. musher' right in front of us, eh?" "heres 0 st. musher, right on the mailbox. that was easier than i expected!" "0 though?" "well, he mustve started early in the day."

_________________
The freedom to NOT run the software, to be free to avoid vendor lock-in through appropriate modularization/encapsulation and minimized dependencies; meaning any free software can be replaced with a user’s preferred alternatives.
Back to top
View user's profile Send private message Visit poster's website 
musher0

Joined: 04 Jan 2009
Posts: 15041
Location: Gatineau (Qc), Canada

PostPosted: Mon 19 Nov 2018, 19:39    Post subject:  

ROFL!
_________________
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 2084
Location: Japan

PostPosted: Tue 20 Nov 2018, 02:29    Post subject:  

Apart from bash uses only date.
I didn't bother to localize "week" in the header.
Unicode line and bold format may not work in some consoles.
Code:
#!/bin/bash
DW=$(date +%w)                                      # day of week
CW=$(date +%U)                                      # week number of today since year start
SW=$(date -d "tomorrow $(date +%d) days ago" +%U)   # week number of 1st day of month since year start
MW=$((CW-SW+1))                                     # week number of today since month start
sunday=$(date -d "$(date +%u) days ago" +%F)        # date of current week's sunday (= first date in line)

date +"%B %Y, week $MW%n──────────────────────────"
echo $(
for i in {0..6}; do
date -d "$sunday + $i day" +%a
done
)

for i in {0..6}; do
((i!=DW))&& format="${format}%3d " || format="${format}\e[1;41;97m%3d\e[0m "
done

printf "$format" $(
for i in {0..6}; do
date -d "$sunday + $i day" +%d
done
)
thisweek.png
 Description   
 Filesize   1.08 KB
 Viewed   233 Time(s)

thisweek.png


Last edited by MochiMoppel on Tue 20 Nov 2018, 06:21; edited 1 time in total
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 15041
Location: Gatineau (Qc), Canada

PostPosted: Tue 20 Nov 2018, 05:16    Post subject:  

Thanks for this, MM.

However, I have to run your script preceded by a LANG variable.
LANG=en_CA or LANG=fr_CA are ok, but not LANG=C and not
LANG=fr_CA.utf8.

Otherwise I get errors. Please see screen capture.
My system language is fr_CA.utf8. Mysterious.

BFN.
thisweek-MM-quite-strange.jpg
 Description   
 Filesize   152.76 KB
 Viewed   202 Time(s)

thisweek-MM-quite-strange.jpg


_________________
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 15041
Location: Gatineau (Qc), Canada

PostPosted: Tue 20 Nov 2018, 05:38    Post subject:  

Hello all.

Here is a simple one-liner inspired by puppy_apprentice's approach,
without coloring:
Code:
cal | head -n 2;cal | tail -n 6 | grep $(date '+%d')
   November 2018
Su Mo Tu We Th Fr Sa
18 19 20 21 22 23 24

BFN.

_________________
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 2084
Location: Japan

PostPosted: Tue 20 Nov 2018, 05:45    Post subject:  

musher0 wrote:
Otherwise I get errors. Please see screen capture.

This should work better:
sunday=$(date -d "$(date +%u) days ago" +%F)
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 2084
Location: Japan

PostPosted: Tue 20 Nov 2018, 06:00    Post subject:  

musher0 wrote:
Here is a simple one-liner inspired by puppy_apprentice's approach,
without coloring:
Code:
cal | head -n 2;cal | tail -n 6 | grep $(date '+%d')
   November 2018
Su Mo Tu We Th Fr Sa
18 19 20 21 22 23 24
.

A bit shorter:
Code:
cal | egrep "[a-z]|$(date +%d)"
   November 2018
Su Mo Tu We Th Fr Sa
18 19 20 21 22 23 24
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 15041
Location: Gatineau (Qc), Canada

PostPosted: Tue 20 Nov 2018, 06:11    Post subject:  

Yep. Thanks MM.

Getting fancy colors, with a mix of approaches and the help of ANSI code
\e[2A (hehe):
Code:
echo -e "\n\t      \e[33m`date '+%b %Y'`";echo -e "\t`cal | head -n 2 | tail -1`\e[0m";cal | tail -n 6 | nl| grep --color=always $(date '+%d');echo;echo -e "\e[2A\e[33mweek\e[0m"
Image of result attached.

BFN.
~~~~~~~~~~~
10 minutes later -- As a proper script --
Code:
#!/bin/bash
# /root/my-applications/bin/fancyWeek.sh
####
echo -e "\n\t      \e[33m`date '+%b %Y'`"
echo -e "\t`cal | head -n 2 | tail -1`\e[0m"
cal | tail -n 6 | nl | grep --color=always $(date '+%d')
echo
echo -e "\e[2A\e[33mweek\e[0m"
Fancy.jpg
 Description   
 Filesize   9.31 KB
 Viewed   186 Time(s)

Fancy.jpg


_________________
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

Last edited by musher0 on Tue 20 Nov 2018, 06:38; edited 1 time in total
Back to top
View user's profile Send private message 
musher0

Joined: 04 Jan 2009
Posts: 15041
Location: Gatineau (Qc), Canada

PostPosted: Tue 20 Nov 2018, 06:22    Post subject:  

MochiMoppel wrote:
musher0 wrote:
Otherwise I get errors. Please see screen capture.

This should work better:
sunday=$(date -d "$(date +%u) days ago" +%F)

Ok now, thanks.
thisweek-MM-not-strange.jpg
 Description   
 Filesize   10.74 KB
 Viewed   171 Time(s)

thisweek-MM-not-strange.jpg


_________________
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)
Back to top
View user's profile Send private message 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Tue 20 Nov 2018, 10:00    Post subject:  

Other variations:

Code:
#!/bin/sh
#without coloring
cal | head -n 2 ; line=$(cal | grep -n " $(date +%d) ") ; echo ${line:2} \(week $((${line:0:1}-2))\)

echo

#with coloring
cal | head -n 2 ; today=$(date +%d) ; line=$(cal | grep -n " $today ") ; echo $(echo ${line:2} | grep --color=always " $today ") \(week $((${line:0:1}-2))\)

echo

# <tail -n 6> won't work for other months?
echo "    " $(cal | head -n 1) ; echo " " $(cal | head -n 2 | tail -1) ; cal | tail -n 6 | grep -n --color=always " $(date +%d) "

echo

#without coloring
cal | head -n 2 ; line=$(cal | tail -n +3 | grep -n " $(date +%d) ") ; echo ${line:2} \(week ${line:0:1}\)

echo

#with coloring
cal | head -n 2 ; today=$(date +%d) ; line=$(cal | tail -n +3 | grep -n " $today ") ; echo $(echo ${line:2} | grep --color=always " $today ") \(week ${line:0:1}\)

echo

#changed <tail -n 6> into <tail -n +3>
echo "    " $(cal | head -n 1) ; echo " " $(cal | head -n 2 | tail -1) ;  cal | tail -n +3 | grep -n --color=always " $(date +%d) "


Above code doesn't find eg. 18 like on picture Wink
Screenshot_2018-11-20_154727.jpg
 Description   
 Filesize   37.12 KB
 Viewed   145 Time(s)

Screenshot_2018-11-20_154727.jpg


Last edited by puppy_apprentice on Tue 20 Nov 2018, 15:16; edited 11 times in total
Back to top
View user's profile Send private message 
nosystemdthanks


Joined: 03 May 2018
Posts: 724

PostPosted: Tue 20 Nov 2018, 10:06    Post subject:  

edit: ok, you fixed it.
Back to top
View user's profile Send private message Visit poster's website 
musher0

Joined: 04 Jan 2009
Posts: 15041
Location: Gatineau (Qc), Canada

PostPosted: Tue 20 Nov 2018, 14:56    Post subject:  

@nosystemdthanks: Euh... Who fixed what, please?!

@all: Do you guys think that we can top Beethoven's 33 Variations (on a
theme by Diabelli)? (tongue-in-cheek icon here!)
We are already at 15 variants for this "This Week" script. Laughing

Last edited by musher0 on Tue 20 Nov 2018, 15:05; edited 2 times in total
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 2 of 7 [97 Posts]   Goto page: Previous 1, 2, 3, 4, 5, 6, 7 Next
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies. View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1604s ][ Queries: 13 (0.0549s) ][ GZIP on ]