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 Sat 14 Dec 2019, 11:28
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 7 of 7 [97 Posts]   Goto page: Previous 1, 2, 3, 4, 5, 6, 7
Author Message
musher0

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

PostPosted: Wed 05 Dec 2018, 04:44    Post subject:  

Sorry to bother you!

I'm sure this insignificant script displaying a month

-- colored with some colors in the 90's register of ANSI codes
-- fading according to in-focus and out-of-focus
-- with the single numbers of the month padded
-- with a little motto at the bottom (hopefully inspiring!)
-- using < rxvt > and < less > to produce a pseudo-GUI
-- multilingual (plus you could add your own locale)
-- with a suggested entry for a pekwm or ae menu

is off-topic and of no interest to anyone! Wink

BFN
hic-et-nunc4-EN.jpg
 Description   
 Filesize   62.99 KB
 Viewed   351 Time(s)

hic-et-nunc4-EN.jpg

hic-et-nunc4.sh.zip
Description 
zip

 Download 
Filename  hic-et-nunc4.sh.zip 
Filesize  2.25 KB 
Downloaded  60 Time(s) 
hic-et-nunc4.jpg
 Description   French version (sorry to impose).
But if LANG variable NOT set to fr, will display in English.
 Filesize   65.43 KB
 Viewed   414 Time(s)

hic-et-nunc4.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 Thu 06 Dec 2018, 19:05; edited 1 time in total
Back to top
View user's profile Send private message 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 300

PostPosted: Thu 06 Dec 2018, 16:59    Post subject:  

MochiMoppel wrote:

With little extra code it is possible to pull dates from a database and mark them in the calendar. These dates can be public holidays, birthdays, dentist appointments or whatever.


I'm working on something similar with coloring done it this way (without curses it will be hard to make windows and menus but i'm thinking to use 'select' or 'tput' command):

Code:
cal | grep -w --color=always "$(seq 1 2 31)" | sed 's/31m/32m/g'


or

Code:
a=$(cal | grep -w --color=always "$(seq 1 2 31)"); echo "${a//31m/34m}"


Code:
cal | grep -w -B 6 -A 6 --color=always 10 | sed 's/31m/32m/g' | grep -w -B 6 -A 6 --color=always 20


where '31' is standard color from grep command.
Back to top
View user's profile Send private message 
Burunduk

Joined: 21 Aug 2011
Posts: 76

PostPosted: Sun 16 Dec 2018, 17:35    Post subject:  

MochiMoppel wrote:
Are you sure? Does not work when the year is 2018 and day is the 18th Wink

Thank you for fixing that nasty bug Smile

I like your idea of using "pipe-separated values" as a part of a regexp. It is also possible to apply this technique to the entire year view:

Code:
#!/bin/bash

# Highlights dates from two sets in the cal command output
# using distinct styles (in the month and year view).
# Requires GNU sed. Busybox sed doesn't understand multibytes chars
# (it seems to be the compile option).
# It also doesn't support s/.../.../Ng options combo
# but this can be worked around:
#
# mark_days="$mark_days
#     $addr,+6 { s/./!/22; s/./!/44; s/...?!?/%$i-&/g
#                :a$i; s/(!.*%)$i/\1$((i+1))/; ta$i; s/!/ /
#                :b$i; s/(!.*%)$((i+1))/\1$((i+2))/; tb$i; s/!/ /
#              }"
#
# Usage: ./this_script [ [month] year ]
# No error checking. This is just a demo.

# cal and busybox cal insert empty lines in different places
CALCMD="busybox cal" addr=4 inc=8
#CALCMD="cal -h" addr=3 inc=9

# Highlight:
# 1st set of days
daysA="1-1|1-7|12-25|12-26|12-31"
# 2nd set of days
daysB="1-2|12-31"
# columns
daysC="1|6|7"

# Styles:
onA=$'\x1b[1;37;42m'    # for the 1st set
offA=$'\x1b[0m'         # turn the style off

onB=$'\x1b[1;35m'       # for the 2nd set
offB="$offA"

onAB=$'\x1b[1;37;45m'   # for the intersection of the two sets
offAB="$offA"

onC=$'\x1b[1;31m'       # for columns (if no other style applies)
offC="$offA"


# Settings for the month and year view
case $# in
    0)  months=`date +%m`
        addr=2
        ;;
    1)  months='1 4 7 10'
        ;;
    2)  months=${1%%[^0-9]*}
        addr=2
        ;;
esac

# Generate repetitive parts of the sed script:

# add month labels before every 3rd char (skipping spaces between months)
mark_days=
for i in $months; do
    mark_days="$mark_days
        $addr,+6 { s/./!/22; s/./!/44; s/...?!?/%$i-&/g; s/!/ /g
                 s/%$i/%$((i+1))/8g; s/%$((i+1))/%$((i+2))/8g
                 } "
    addr=$((addr+inc))
done

# add style labels to the specified columns
mark_columns=
for i in ${daysC//|/ }; do
    mark_columns="$mark_columns
        $(printf "s/%%/C%%/%i;" $i $((i+7)) $((i+14)) )"
done

# Apply styles

$CALCMD "$@" | sed -r "

    $mark_days

    $mark_columns

    # mark days from the sets
    s/- /-/g
    s/%($daysA)( |$)/A&/g
    s/%($daysB)( |$)/B&/g
    s/-(.( |$))/- \1/g

    # replace marks with the actual style markup
    s/C?AB%..?-(..)/$onAB\1$offAB/g
    s/C?A%..?-(..)/$onA\1$offA/g
    s/C?B%..?-(..)/$onB\1$offB/g
    s/C%..?-([^ ].| [^ ])/$onC\1$offC/g    # or /C%..?-(..)/
    s/C?%..?-(..)/\1/g    # remove unused marks

    "


===
musher0 wrote:
I just did a < busybox --list > on my xenialPup, and it does not have a
< busybox awk >.

Probably the easiest way to test busybox awk is by downloading a statically compiled binary from the official site.
https://busybox.net/downloads/binaries
Code:
root# busybox awk
awk: applet not found
root# chmod 755 busybox-i686
root# ./busybox-i686 awk
BusyBox v1.28.1 (2018-02-15 14:34:02 CET) multi-call binary.

Usage: awk [OPTIONS] [AWK_PROGRAM] [FILE]...

   -v VAR=VAL   Set variable
   -F SEP      Use SEP as field separator
   -f FILE      Read program from FILE
   -e AWK_PROGRAM
colorcal.png
 Description   colored cal command
 Filesize   12.61 KB
 Viewed   316 Time(s)

colorcal.png

Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1947
Location: Japan

PostPosted: Tue 25 Dec 2018, 01:54    Post subject:  

Burunduk wrote:
It is also possible to apply this technique to the entire year view
Well done!
I never thought that anyone would try to do this Laughing

Quote:
Busybox sed doesn't understand multibytes chars
Well, it understands multibyte chars when inserted directly or via variable, not when the chars are represented by escape codes. But I don't see such chars in your code. I only see that busybox sed doesn't work. Can you point me to an expression where busybox sed fails?

Quote:
Probably the easiest way to test busybox awk is by downloading a statically compiled binary from the official site.
https://busybox.net/downloads/binaries
These binaries seem to be compiled with CONFIG_LOCALE_SUPPORT not enabled. They are English only. It would be great if you or anyone else knows where to download a recent busybox with support for locales.
Back to top
View user's profile Send private message 
Burunduk

Joined: 21 Aug 2011
Posts: 76

PostPosted: Sun 30 Dec 2018, 16:58    Post subject:  

OK, this is a bit late respond but anyway...

MochiMoppel wrote:
Well, it understands multibyte chars when inserted directly or via variable, not when the chars are represented by escape codes. But I don't see such chars in your code. I only see that busybox sed doesn't work. Can you point me to an expression where busybox sed fails?


There are several problems with busybox here.

Multibyte characters may appear in the cal command output and what busybox sees is a bytestring. This means that I can't use dots in the regexps.

Code:
# echo "żółw" | busybox sed 's/./1/g' | wc -c
8
# echo "żółw" | sed 's/./1/g' | wc -c
5


Now, a command like s/a/b/5g fails because busybox ignores the number and replaces all the matches.

And the old versions of busybox have a bug: s/a/b/5; results in an unrecognized option error while s/a/b/5 ; (with a space before ; ) works.

And addresses like 1,+2 aren't supported by the old busybox versions.

I've rewritten the script so it should work with busybox now.

Code:
#!/bin/busybox sh

# Highlights dates from two sets in the cal command output
# using distinct styles (in the month and year view).
# This version outputs an HTML document.
# Should work with busybox utilities.
# Usage: ./this_script [ [month] year ] >cal.html
# No error checking. This is just a demo.

# cal and busybox cal insert empty lines in different places
CALCMD="busybox cal" addr=4 inc=8
#CALCMD="/usr/bin/cal -h" addr=3 inc=9

# Highlight:
# 1st set of days
daysA="1-1|1-7|12-25|12-26|12-31"
# 2nd set of days
daysB="1-2|12-31"
# columns
daysC="1|6|7"

# Styles:
# for terminal ---
#before=
#after=
#
#onA=$'\x1b[1;37;42m'    # for the 1st set
#offA=$'\x1b[0m'         # turn the style off
#
#onB=$'\x1b[1;35m'       # for the 2nd set
#offB="$offA"
#
#onAB=$'\x1b[1;37;45m'   # for the intersection of the two sets
#offAB="$offA"
#
#onC=$'\x1b[1;31m'       # for columns (if no other style applies)
#offC="$offA"
# ---

# for html ---
before="
<!DOCTYPE html>
<html><head>
<title>Kalendarz</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
<style>
.a { color: olive }
.b { color: snow; background-color: springgreen }
.ab { color: mintcream; background-color: orchid }
.c { color: tomato }
</style>
</head><body>
<pre>
"
after="
</pre>
</body></html>
"

onA='<span class=\"a\">'
offA='<\/span>'

onB='<span class=\"b\">'
offB="$offA"

onAB='<span class=\"ab\">'
offAB="$offA"

onC='<span class=\"c\">'
offC="$offA"
# ---


# Settings for the month and year view
case $# in
    0)  months=`date +%m`
        addr=2
        ;;
    1)  months='1 4 7 10'
        ;;
    2)  months=${1%%[^0-9]*}
        addr=2
        ;;
esac

# Generate repetitive parts of the sed script:

# add month labels before every 3rd char (skipping spaces between months)
mark_days=
for i in $months; do
    mark_days="$mark_days
        $addr s/[^ ]+/%-&/g
        $((addr+1)),$((addr+6)) { s/./!/22 ; s/./!/44 ; s/...?!?/%$i-&/g
            :a$i; s/(!.*%)$i/\1$((i+1))/; ta$i; s/!/ /
            :b$i; s/(!.*%)$((i+1))/\1$((i+2))/; tb$i; s/!/ /
            # s commands here are repeated until they fail
        }"
    addr=$((addr+inc))
done

# add style labels to the specified columns
mark_columns=
for i in ${daysC//|/ }; do
    mark_columns="$mark_columns
        $(printf "s/%%/C%%/%i ; " $i $((i+7)) $((i+14)) )"
done

# Apply styles

echo -n "$before"

$CALCMD "$@" | sed -r "

    $mark_days

    $mark_columns

    # mark days from the sets
    s/- /-/g
    s/%($daysA)( |$)/A&/g
    s/%($daysB)( |$)/B&/g
    s/-(.( |$))/- \1/g

    # replace marks with the actual style markup
    s/C?AB%..?-(..)/$onAB\1$offAB/g
    s/C?A%..?-(..)/$onA\1$offA/g
    s/C?B%..?-(..)/$onB\1$offB/g
    s/C%[^-]*-( ?[^ ]+)( |$)/$onC\1$offC\2/g
    s/C?%[^-]*-//g    # remove unused marks

    "

echo -n "$after"
Back to top
View user's profile Send private message 
musher0

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

PostPosted: Sun 30 Dec 2018, 19:20    Post subject:  

Hello all.

Please find attached a variant of my "completely useless" Wink DB-type
approach above:
Code:
#!/bin/bash
# /root/my-applications/bin/mois_zeros+espaces.sh
# Sketch by musher0, 26 Nov. 2018. GPL3. # Rev. 30 déc. 2018.
#
# Requires awk, replaceit, tr, and paste.
#
# Syntax should be < mois_zeros_espaces.sh month year >, but there
# is a safeguard: it defaults to current month and year if the
# script finds no positional parameters.
#
# Please read NOTE at bottom.
####

[ "$1" ] && A=$1 || A="`date '+%m'`" # mois
[ "$2" ] && B=$2 || B="`date '+%Y'`" # année
TmP=/root/my-documents/tmp
MoiS="$TmP/mois.txt"
RPLCT="replaceit --input=$MoiS"

EnTete="`cal $A $B | grep -vE [[:digit:]] | tr " " "\t"`"
echo " " > $MoiS # For better appearance in the end.
cal $A $B | grep --color=always -vE [[:alpha:]] | tr " " ";" >> $MoiS
echo $MOIS

num="";for num in {1..9};do
     $RPLCT ";;$num" ";0$num";$RPLCT ";$num;" "0$num;"
done

i=1;for i in {1..7};do
     $RPLCT ";;;" " o "
     i="$[$i+i]"
done
# Maintenant on a un mois avec des dates de même longueur,
# tous séparés par un espace. /
# Now we have a month with dates of same length, all
# separated by a space.

cat $MoiS | tr ";" " "  > $TmP/mois.txt2

for i in {1..7}; do # séparation en colonnes (simili-BdD)
     awk '{ print $'$i' }' $TmP/mois.txt2 | tr "o" "-" > $TmP/jours$i.txt
done
for j in 1 7;do
   > $TmP/jours$j.txt2
   while read line;do
      echo -e "\e[32m$line\e[0m" >> $TmP/jours$j.txt2
   done < $TmP/jours$j.txt
   mv $TmP/jours$j.txt2 $TmP/jours$j.txt
done

# Affichage
clear
echo; echo -e "\t\t\t\e[1;33m`cal $A $B | head -1`\e[0m"
paste /root/my-applications/bin/taquets.txt $TmP/jours*.txt
echo -e "\n\t\e[1;33m$EnTete\e[0m"

# Nettoyage
rm -f $TmP/mois.tx*
read
rm -f $TmP/jours*

exit

## NOTE ##
# To run in its own window, in your Puppy's locale:
# urxvt +sb -bg "#352e20" -g 67x13 -e mois_zeros+espaces.sh
# To run in its own window, in English:
# LANG=en_CA;urxvt +sb -bg "#352e20" -g 67x13 -e mois_zeros+espaces.sh
##########
mois_zeros+espaces.sh.zip
Description  (Same as above, zipped. Pls remember to make executable before running.)
zip

 Download 
Filename  mois_zeros+espaces.sh.zip 
Filesize  1.17 KB 
Downloaded  47 Time(s) 
month+spaces-bil.jpg
 Description   
 Filesize   69.29 KB
 Viewed   176 Time(s)

month+spaces-bil.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 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1947
Location: Japan

PostPosted: Mon 31 Dec 2018, 02:10    Post subject:  

Burunduk wrote:
This means that I can't use dots in the regexps.
Code:
# echo "żółw" | busybox sed 's/./1/g' | wc -c
8
# echo "żółw" | sed 's/./1/g' | wc -c
5
Are you by any chance using the busybox version you recommended for download? As you've already noticed and I said before: this version is English only. Any of the Puppy busybox versions should produce the correct result of 5.


Quote:
Now, a command like s/a/b/5g fails because busybox ignores the number and replaces all the matches.
It doesn't "fail", it only tries to make sense of an ambiguous command. What do you expect it to produce when you instruct it to a) replace only the 5th match? and b) replace all matches ?

When you use two contradicting flags, newer busyboxes ignore the first flag and honor only the last:
Code:
# echo a..a...a...a..a | busybox sed s/a/X/3g               
X..X...X...X..X


If you change the order of the flags in the example, busybox will ignore 'g':
Code:
# echo a..a...a...a..a | busybox sed s/a/X/g3
a..a...X...a..a
This is also the result I get from my older busybox 1.21.0, regardless of the g3 order.
GNU sed output is still different. Matches from the 3rd to the last are replaced, matches before the 3rd are ignored.

Bottomline: avoid ambiguous commands.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 7 of 7 [97 Posts]   Goto page: Previous 1, 2, 3, 4, 5, 6, 7
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.0832s ][ Queries: 13 (0.0274s) ][ GZIP on ]