Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Thu 23 May 2019, 07:22
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
"This Week" script: in bash, awk, or whatever.
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 7 [97 Posts]   Goto page: 1, 2, 3, 4, 5, 6, 7 Next
Author Message
musher0

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

PostPosted: Sun 18 Nov 2018, 22:10    Post subject:  "This Week" script: in bash, awk, or whatever.
Subject description: Variations that are completely useless for sane people!
 

Hi, people.

As the title and sub-title say. Nerds may get off it, though!
Code:
#!/bin/bash
# /root/my-applications/bin/ThisWeek.sh
#
# Purpose: Show this week's dates.
#
# Result --
# Shows the abbreviations of the week days
# in the system's default language on top,
# Then on the 2nd line, it shows the week
# number for the month, a tab and the
# dates for that week.
#
# Required: awk, busybox cal, nl.
#
# © Christian L'Écuyer, Gatineau (Qc), Canada, 18 nov. 2018. GPL3.
# (Alias musher0 [forum Puppy].) # Rév. : None yet.
####

# case "${LANG:0:2}" in
#   en)echo blabla > /dev/null ;;
#   *)LangVx="$LANG"
#      export LANG=en_CA ;;
# esac
# Comment out the five lines above if you wish to have
# the output in the system's default language, if the
# system's default language is not English.

echo -e "\n\t\e[35m\e[4m`busybox cal | grep -v '[[:digit:]]'`\e[1A\e[0m"
busybox cal | grep -v '[[:alpha:]]' | nl -w2 -nln | awk '$0 ~ /18/'
# If for a strange reason you do not have busybox on your system,
# replace the < busybox cal > command above with the syntax for the
# GNU cal, which is < cal -s >.

# [ "${LangVx}" != "" ] && export LANG="$LangVx"
# Comment out the line above to restore the original LANG var., if need be.

Feel free to improve, keep or chuck!

Screenshot of result, for this month of November 2018, is attached.

BFN.
ThisWeek-example.jpg
 Description   
 Filesize   20.83 KB
 Viewed   363 Time(s)

ThisWeek-example.jpg


_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)

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


Joined: 03 May 2018
Posts: 623

PostPosted: Mon 19 Nov 2018, 00:48    Post subject:  

heres my version:

p=$(date | cut -f3 -d ' ') ; cal | egrep "Su|$p" | egrep -v "1$p|2$p|3$p" | tail -2 # public domain

output:
Code:
Su Mo Tu We Th Fr Sa
18 19 20 21 22 23 24


heres that unpacked:

p=$(date | cut -f3 -d ' ') # get the day of month

cal # run cal like you did

| egrep "Su|$p" # get day names and any lines with day

| egrep -v "1$p|2$p|3$p" # if day is 8, will remove 18, 28, 38 from listing. if it is 18, it will try to remove 118, 218 and 318

| tail -2 # if the month line is still there, only get the bottom 2 to remove the month line

this should work since a week has fewer than 10 days. however, id like to write a test program to be sure.

note i basically looked at your output and didnt read through your code at first-- my reaction was "this could be a one-liner" but yours basically is, and i realise you added some extra features.

edit: not as easy as it looks:

Code:
p=$(date | cut -f3 -d ' ') ; c=$(cal | egrep "Su|$p" | egrep -v "1$p|2$p|3$p|J|Fe|Ma|A|Se|O|N" | tr '\n' @ | tr ' ' '-') ; echo $c | tr '@' '\n' | tr - ' ' | head -1 ; echo $c | tr @ '\n' | egrep -v "1$p|2$p|3$p" | grep "[[:digit:]]" | tail -1 | tr - ' ' # public domain


this is more reliable, i checked it like this:

Code:
for p in $(seq 1 31) ; do c=$(cal | egrep "Su|$p" | egrep -v "1$p|2$p|3$p|J|Fe|Ma|A|Se|O|N" | tr '\n' @ | tr ' ' '-') ; echo $c | tr '@' '\n' | tr - ' ' | head -1 ; echo $c | tr @ '\n' | egrep -v "1$p|2$p|3$p" | grep "[[:digit:]]" | tail -1 | tr - ' ' ; echo ; done # public domain


well, crap...

i cede that your code is more reliable-- i could use it as inspiration for fixing mine, though at that point mine would be so much more like yours, that its only an exercise in stealing.

testing your code:

Code:
leafpad <(for mo in Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ; do for p in $(seq 1 31) ; do echo -e "\n\t\e[35m\e[4m`cal $mo 2018 | grep -v '[[:digit:]]'`\e[1A\e[0m" ;  cal $mo 2018 | grep -v '[[:alpha:]]' | nl -w2 -nln | awk '$0 ~ /18/' ; echo ; done ; done # gpl3
)


you did good with this one.

_________________
communities (sometimes) care what youre going through as a user. big for-profit corporations never do.
Back to top
View user's profile Send private message Visit poster's website 
musher0

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

PostPosted: Mon 19 Nov 2018, 04:13    Post subject:  

Thanks for taking a look at it and for testing alternatives,
nosystemdthanks.

Here's my 2nd iteration for it --

Not terribly different to an uneducated eye looking at the pseudo-GUI.

But in this one, the thing is dumped to a file, and that file read with a
while-do-done structure to include tabs.

The week # is pushed at the end of the line; makes the beginning of the
line look more "flush" to the left.

Added the month as header. It reduces the guessing!

Also trying to imitate an overbar (aka overline), with so-so results.
I found the Unicode #, but Q.: how to make it affect a CLI output?
If anybody knows the answer to this one, please chime in. TIA.

I've included an aemenu "leg", under the code proper, if anyone wants
it, to pop up the pseudo-GUI.

So here you go --
Code:
#!/bin/bash
# /root/my-applications/bin/ThisWeek.sh
# Lien : /root/my-applications/bin/CetteSemaine
#
# Purpose: Show the week's dates.
# Objectif : Montrer les dates de la semaine.
#
# Result --
# First shows the name of the month and the
# abbreviations of the week days in the system's
# default language. Then it shows the dates for
# the week, a separator, and the week number.
#
# Résultat --
# Montre d'abord le nom du mois et les abréviations
# des jours dans la langue par défaut du système.
# Puis les dates de la semaine, un séparateur et
# le numéro de la semaine.
#
# Required / Requis : awk, busybox cal, nl & regex.
#
# © Christian L'Écuyer, Gatineau (Qc), Canada, 18 nov. 2018.
# (Alias musher0 [forum Puppy].) # Rév. : 19 nov. 2018.
# GPL3. S.v.p. voir ci-dessous. / Please see below.
####
Ligne () { echo -e "\e[33m-- -- -- -- -- -- --\e[0m" >> temporaire; }

# if [ "${LANG:0:2}" != "en" ];then
#    LangVx="$LANG";export LANG=en_CA
# fi
# Comment out the three lines above if you wish to have
# the output in the system's default language, if the
# system's default language is not English.

case "${LANG:0:2}" in fr)Sem="sem." ;;
     *)Sem="wk" ;;
esac

TodaY="`date '+%d'`" # echo $TodaY # Line added.

> temporaire
echo -e "\n\e[33m`busybox cal | grep '[[:alpha:]]'`" >> temporaire # \e[1A # \e[4m
Ligne
busybox cal | grep -v '[[:alpha:]]' | nl -w2 -nln | awk '$0 ~ /'$TodaY'/ { print $2,$3,$4,$5,$6,$7,$8" -- '$Sem' "$1}' >> temporaire
# Line edited.
Ligne

while read line;do
     echo -e "\t$line"
done < temporaire
read
rm -f temporaire

# If for a strange reason you do not have busybox on your system,
# replace the < busybox cal > command above with the syntax for
# the GNU cal, which is < cal -s >.

# [ "${LangVx}" != "" ] && export LANG="$LangVx"
# Comment out the line above to restore the original LANG var., if need be.
exit

Notes --
Pseudo-GUI --
rxvt -g 45x8+200+200 +sb -tr -tint snow -sh 50 -fn xft:LiberationMono:pixelsize=15:antialias=true:autohint=true -e CetteSemaine

Line for an aemenu --
cmd \"    Week\" \"rxvt -g 45x8+200+200 +sb -tr -tint snow -sh 50 -fn xft:LiberationMono:pixelsize=15:antialias=true:autohint=true -e CetteSemaine\"

Licence --
#################   # https://opensource.org/licenses/GPL-3.0
This program is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or  (at your option) any later version.
     This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
     You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
##########
Ce programme est libre : vous pouvez le redistribuer ou modifier selon les termes
de la Licence Publique Générale GNU publiée par la Free Software Foundation (v. 3
ou toute version ultérieure choisie par vous).
    Ce programme est distribué dans l''espoir qu''il sera utile, mais SANS AUCUNE
GARANTIE, ni explicite ni implicite, y compris des garanties de commercialisation
ou d''adaptation à un but spécifique. Pour plus de détails, veuillez vous reporter
au texte officiel de cette licence à https://opensource.org/licenses/GPL-3.0, à
http://www.linux-france.org/article/these/gpl.html pour une traduction et, pour une
explication en français, à https://fr.wikipedia.org/wiki/Licence_publique_générale_GNU.
################

Again feel free to improve, keep as is or chuck.
Scrot of pseudo-GUI appears below.

BFN.

~~~~~~~~~~~~~~~~~~
P. S., 5 min. later --
Shucks. That /18/ in the awk line should be today's date... As
nosystemdthanks intuited. Corresponding lines edited in the above.
ThisWeek-example2.png
 Description   
 Filesize   74.63 KB
 Viewed   319 Time(s)

ThisWeek-example2.png


_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
step

Joined: 04 May 2012
Posts: 1176

PostPosted: Mon 19 Nov 2018, 12:54    Post subject:
Subject description: awk
 

I didn't do the gui, just the first part (header)
Save as ./weekcal.awk
Code:

#!/usr/bin/awk -f

# Note: week of the month according to ISO 8601 week dates

BEGIN {
   day = 3600 * 24 # seconds
   now = systime()
   first_day_month = mktime(strftime("%Y %m 1 0 0 0", now, 1))
   week_of_month = strftime("%V", now) - strftime("%V", first_day_month) + 1
   start_day_week = 3 # was a Sunday
   
   printf "\t\t"
   for(i = start_day_week; i <= start_day_week + 6; i++)
      printf "% 4s", strftime("%a", i * day, 1)

   printf "\n% 8d\t", week_of_month
   offset_from_sunday = strftime("%w")
   for(i = 0 - offset_from_sunday; i <= 6 - offset_from_sunday; i++)
      printf "% 4d", strftime("%d", now + i * day, 1)

   print ""
   exit
}

Make weekcal.awk executable and run it: ./weekcal.awk. However, it isn't a bash script so it doesn't qualify. Or does it?

Save as weekcal.sh
Code:

#!/bin/bash
exec awk -f weekcal.awk

Make it executable and run it. ./weekcal.sh Smile

I don't know if it works with non-English locales because I only have English installed. Can you try and let me know? Thanks.
Also, days are abbreviated with three letters. Changing the script to display two letters isn't too difficult.

edited to start the week on Sunday
edited to add French picture after installing system locale fr_CA
weekcal-fr.png
 Description   
 Filesize   5.1 KB
 Viewed   204 Time(s)

weekcal-fr.png

weekcal.png
 Description   
 Filesize   4.34 KB
 Viewed   234 Time(s)

weekcal.png


_________________
Fatdog64-801|+Packages|Kodi|Findnrun|+forum|gtkmenuplus

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

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

PostPosted: Mon 19 Nov 2018, 14:45    Post subject:  

Hi step.

Thanks for this.
Sure an awk script qualifies! I've changed the title of the thread.

I'll try it with my locale and get back to you.

Again thanks.

~~~~~~~~~
10 min. later:

-- I had to change line 1 to
Code:
#!/bin/awk -f
to get your script to work. That's where my (m)awk is.

-- Please see attached output on my LANG=fr_CA Puppy.
Q.: Does awk have a way to change the locale as needed?

-- Three-letter days abbreviations pose no problem to me. Is anyone
complaining?

-- Hm. Last Sunday (yesterday) was the 18th of Nov., not the 19th.

IHTH. BFN.
weekcal-1.jpg
 Description   
 Filesize   24.16 KB
 Viewed   262 Time(s)

weekcal-1.jpg


_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
nosystemdthanks


Joined: 03 May 2018
Posts: 623

PostPosted: Mon 19 Nov 2018, 15:28    Post subject:  

#just for fun, in fig:

cal = "busybox cal" ; arrshell

function pad (s)
now = " " plus s plus " " ; return now
next

mnth = cal ; mid 1 1 ; colourtext 6 ; print
week = cal ; mid 2 1 ; print
days = cal ; mid 3 10 # python-like lack of penalty for going past array bound

border = "-- " times 7 ; print ; colourtext 7

forin row, days

weekdays = pad row
day = date ; split day, "/" ; mid 2 1 ; pad day
found = instr weekdays, day

iftrue found
now = row ; prints ; " -- sem. " ; prints
sem = instr days, row ; print
fig

next

now = border ; colourtext 6 ; print ; colourtext 7

#### license: creative commons cc0 1.0 (public domain)
#### http://creativecommons.org/publicdomain/zero/1.0/
cal.png
 Description   
 Filesize   4.97 KB
 Viewed   262 Time(s)

cal.png


_________________
communities (sometimes) care what youre going through as a user. big for-profit corporations never do.
Back to top
View user's profile Send private message Visit poster's website 
musher0

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

PostPosted: Mon 19 Nov 2018, 16:45    Post subject:  

Hi systemdthanks.

"November", but "sem."? Methinks you spent too much time with bilingual
French-Canadians lately. (Now where's that tongue-in-cheek icon?!?!) Laughing

BFN.

_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
nosystemdthanks


Joined: 03 May 2018
Posts: 623

PostPosted: Mon 19 Nov 2018, 17:01    Post subject:  

musher0 wrote:
"November", but "sem."?


well, lets think about it-- i think languages are cool, and i know what "sem" is because i wrote the code that puts the number next to it.

no one is going to use this program, though you would probably run it with french language settings if at all. since those dont affect the line that says "sem" that part wont change, but the rest might.

youre not going to use the program either (youve got your own version) but if someone else does, they can change the three letters to something else. thats easier than digging up language settings and using it to get the correct word for "week."

so yes, "sem" -- et pas de problème, naturellement! as a postscript, i nearly had children with a woman that spoke three languages, including french. so did her mother-- when they spoke on the phone, they used all three somewhat randomly mixed up-- unlike in canada, where they say everything twice (thats a joke of course. i used to live where they did the same with english and gaelic.) https://www.youtube.com/watch?v=5HyDHGRsDy8&t=37

_________________
communities (sometimes) care what youre going through as a user. big for-profit corporations never do.
Back to top
View user's profile Send private message Visit poster's website 
step

Joined: 04 May 2012
Posts: 1176

PostPosted: Mon 19 Nov 2018, 17:28    Post subject:  

Hi musher0, thanks for catching that. I have updated the script and the picture.
I don't think awk has a special way to set the locale. It should just follow system locale. However, in this case I suspect LANG doesn't matter. I think that LC_TIME defines day names.

edit, no LANG is correct, my system was missing locale fr_CA

_________________
Fatdog64-801|+Packages|Kodi|Findnrun|+forum|gtkmenuplus

Last edited by step on Mon 19 Nov 2018, 18:30; edited 1 time in total
Back to top
View user's profile Send private message 
step

Joined: 04 May 2012
Posts: 1176

PostPosted: Mon 19 Nov 2018, 17:37    Post subject:  

@nosystemdthank fig?
_________________
Fatdog64-801|+Packages|Kodi|Findnrun|+forum|gtkmenuplus
Back to top
View user's profile Send private message 
nosystemdthanks


Joined: 03 May 2018
Posts: 623

PostPosted: Mon 19 Nov 2018, 17:54    Post subject:  

step wrote:
@nosystemdthank fig?


https://www.bitchute.com/video/YKBkuQumKRUY/

i spent about a quarter of a century using dos and basic, and at least five looking for "the basic of the 21st century." not easy, since most modern versions of basic tend to go crazy about features and even deprecate the easier-to-use ones.

meanwhile, python is really good for teaching and bash is a good shell language, but i wasnt entirely satisfied with either (i still use both. fig is implemented in python.)

fig takes at least half its inspiration from basic, some from logo/turtle (it has a lot of optional syntax and only "quotes for strings" and # hashes for comments are mandatory) and it has many of the advantages of python (including inline python.) i use it for teaching coding and computer-science-for-everyone (how to understand computers/digital humanities in the easiest way possible, rather than just doing application training and leaving computers mysterious and magical.) unlike python, indents are not required (nor braces) and it is not case-sensitive. pet package: http://murga-linux.com/puppy/viewtopic.php?mode=attach&id=99110 and this thing: https://imgur.com/oZ0zc1M

_________________
communities (sometimes) care what youre going through as a user. big for-profit corporations never do.

Last edited by nosystemdthanks on Mon 19 Nov 2018, 18:01; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website 
musher0

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

PostPosted: Mon 19 Nov 2018, 17:59    Post subject:  

Thanks for the edit, step.

@nosystemdthanks:
you can be a roar, you know that!? Very Happy

Everybody understands that the above line is meant as a joke, yes? Laughing

BFN

_________________
musher0
~~~~~~~~~~
Je suis né pour aimer et non pas pour haïr. (Sophocle) /
I was born to love and not to hate. (Sophocles)
Back to top
View user's profile Send private message 
nosystemdthanks


Joined: 03 May 2018
Posts: 623

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

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.

_________________
communities (sometimes) care what youre going through as a user. big for-profit corporations never do.
Back to top
View user's profile Send private message Visit poster's website 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 294

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

My try:

Code:
#!/bin/sh
cal | head -n 2
date=$(date)
line=$(cal | grep -n ${date:4:3})
echo $(cal | grep --color=always ${date:4:3}) \(week $((${line:0:1}-2))\)


Code:
#!/bin/sh
cal | head -n 2
today=$(date +%d) # suggested from mushero
line=$(cal | grep -n " $today")
echo $(cal | grep --color=always " $today") \(week $((${line:0:1}-2))\)


edit: could give strange answer when day will have one digit.
Screenshot_2018-11-19_220454.jpg
 Description   simple with actual day colored
 Filesize   25.12 KB
 Viewed   207 Time(s)

Screenshot_2018-11-19_220454.jpg


Last edited by puppy_apprentice on Mon 19 Nov 2018, 19:49; edited 3 times in total
Back to top
View user's profile Send private message 
step

Joined: 04 May 2012
Posts: 1176

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

@musher0, you're correct, LANG=fr_CA is enough. I was missing that locale. I installed it and ran
LANG=fr_CA ./weekcal.awk to get French output. I added a picture to my first post.

_________________
Fatdog64-801|+Packages|Kodi|Findnrun|+forum|gtkmenuplus
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 7 [97 Posts]   Goto page: 1, 2, 3, 4, 5, 6, 7 Next
Post new topic   Reply to topic 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.0618s ][ Queries: 12 (0.0073s) ][ GZIP on ]