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 20 Jun 2019, 19:39
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 6 of 7 [97 Posts]   Goto page: Previous 1, 2, 3, 4, 5, 6, 7 Next
Author Message
musher0

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

PostPosted: Sat 24 Nov 2018, 07:14    Post subject:  

Hello.

No, MM, I did not understand all you are doing in that script!

After much groping in the dark, using fuzzy logic Sad Laughing
Code:
cal 6 2011 | rev | awk 'NR ~ /2|3/ { print $2 }' | rev
Since we know that day=3 is at the beginning of the month, and that line
2 has the names, it is likely that the 3rd day of the month will be in line 3.

$(NF-1) is giving errors when the month is parsed straight,
(first line of dates does not have enough fields)
so we reverse the listing.
Then we fish out the needed fields and
reverse the result again so it's readable.

I would need a way to iterate through the first line and find the position
of $day. Then it would be less amateurish.

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 
some1

Joined: 17 Jan 2013
Posts: 99

PostPosted: Sat 24 Nov 2018, 18:43    Post subject:  

Quote:
Then it would be less amateurish.



You probably have an empty line in output -> NF NOT set.
Try
Code:
 awk 'NF>0{print $(NF-1);}'
Back to top
View user's profile Send private message 
musher0

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

PostPosted: Sat 24 Nov 2018, 22:01    Post subject:  

Thanks, some1.

I'll keep that on the back burner for now, because I think I have found, in
the meantime, a "awk-less" variant to MM's puzzle, although not as elegant
as his. But it taught me how to juggle variables from array to string.
Code:
date=3;A=(`cal 6 2011 | grep -vE [[:alpha:]] | rev`);for pos in {0..6};do [ "$date" = "${A[$pos]}" ] && POS=$pos;done;B=(`cal 6 2011 | grep -vE [[:digit:]] | rev`);day="`echo ${B[$POS]} | rev`";echo "$day $date"
Result:
Quote:
fr 3
What do you think?

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 
rufwoof


Joined: 24 Feb 2014
Posts: 3161

PostPosted: Sat 24 Nov 2018, 23:15    Post subject:  

Busybox date has a -D option
Code:
busybox date -D "%d%m%Y" -d "03062011" +"%a"

... returns Fri

_________________
( ͡° ͜ʖ ͡°) :wq
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1842
Location: Japan

PostPosted: Sun 25 Nov 2018, 00:52    Post subject:  

rufwoof wrote:
Busybox date has a -D option
Sure but GNU date has not and you don't need this option here anyway.
Code:
#!/bin/bash
day=3
month=6
year=2011
date -d "$year-$month-$day" +"$day/$month/$year is %a"
Back to top
View user's profile Send private message 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 294

PostPosted: Sun 25 Nov 2018, 06:22    Post subject:  

musher0 wrote:
What do you think?


When i'm changing dates in your script i have wrong solutions. Or maybe i'm doing something wrong?

I made another versions. Not short like Mochi's (Hail to the Caesar Wink):

1.
Code:
#!/bin/sh
#shorter version of my earlier script
day=$1
month=$2
year=$3

# array with names of days
nod=($(cal $month $year | head -2 | tail -1))

# number of days in week that include $day
cdiw=$(cal $month $year | grep -w $day | wc -w)

# number of days in week before $day
dbd=$(($(cal $month $year | grep -w "$day" | tr ' ' '\n' | grep -wB 7 "$day" | wc -w)-1))

if (( $cdiw < 7 )) && (( $day < 7 ));
then
   echo "$day/$month/$year is ${nod[7-$cdiw + $dbd]}"
else
   echo "$day/$month/$year is ${nod[dbd]}"
fi


2.
Code:
#!/bin/sh
# idea of use pr command
day=$1
month=$2
year=$3

# vertical list of week names
n=$(cal $month $year | head -2 | tail -1 | tr ' ' '\n')

# store whole week which include $day
k=$(cal $month $year | tail -n +3 | grep -w $day)

# Mochi's stuff, strip 3 spaces and 2 spaces to 1 space
k=${k//   /  }
k=${k//  / }

# vertical list of week which include $day
k=$(echo "$k" | tr ' ' '\n')

#vertical lists $n and $k together
dow=$(echo -e "$n\n$k" | pr -t -s" " --column 2 | grep -w $day)

echo "$dow/$month/$year"
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1842
Location: Japan

PostPosted: Sun 25 Nov 2018, 22:43    Post subject:  

I also made a new version. Shorter, faster and simpler. The ugly loop is gone.
After all a stupid and completely useless script should at least look good Wink
Code:
#!/bin/bash
day=3
month=6
year=2011

CAL=$(cal $month $year)
CAL=${CAL#*$'\n'}
CAL=${CAL//   / x }
CAL=(${CAL%%$day*})
CNT=${#CAL[@]}
echo "$day/$month/$year is ${CAL[$((CNT%7))]}"
Back to top
View user's profile Send private message 
musher0

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

PostPosted: Sun 25 Nov 2018, 23:25    Post subject:  

Ahem.

I did say "completely useless" in the sub-title, but I did not say "stupid".
Your take on it, MM! Very Happy

Besides, is there was a way one could click on the date, and maybe a diary
or schedule opened? Then it would stop being useless.

Is there? I mean: is there a way one can click on a character in a
CLI program, and some other CLI program or script pops up?

And a fading echo repeated: "CLI program, CLI progr...". 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 
puppy_apprentice


Joined: 07 Feb 2012
Posts: 294

PostPosted: Mon 26 Nov 2018, 03:04    Post subject:  

MochiMoppel wrote:
After all a stupid and completely useless script should at least look good Wink


Nothing is stupid if you can learn from it. I've learned some tricks from you. Without those 'stupid' scripts i would have to search those stuff in manuals. Now i have got working examples. Wink

Working nice in MobaXTerm in Windows (attached picture).
https://mobaxterm.mobatek.net/
Bash_in_Windows.jpg
 Description   Mochi's script in action!
 Filesize   91.17 KB
 Viewed   301 Time(s)

Bash_in_Windows.jpg

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

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

PostPosted: Mon 26 Nov 2018, 03:50    Post subject:  

More uselessness! Laughing
Code:
#!/bin/bash
# /root/my-applications/bin/mois_zeros+espaces.sh
# Sketch by musher0 26 Nov. 2018. GPL3.
# Requires awk, replaceit, tr, and paste.
# (Not tomato paste, silly!!! The utility!).
####
# 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.
[ "$1" ] && A=$1 || A="`date '+%m'`" # mois
[ "$2" ] && B=$2 || B="`date '+%Y'`" # année

EnTete="`cal $A $B | grep -vE [[:digit:]]`"
echo " " > mois.txt # For better appearance in the end.
cal $A $B | grep -vE [[:alpha:]] | tr " " ";" >> mois.txt
num="";for num in {1..9};do
   replaceit --input=mois.txt ";;$num" ";0$num"
done
num="";for num in {1..9};do
   replaceit --input=mois.txt ";$num;" "0$num;"
done 
i=1
for i in {1..7};do
   replaceit --input=mois.txt ";;;" "oo "
   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.

echo $EnTete >> mois.txt
cat mois.txt | tr ";" " "  > mois.txt2

for i in {1..7}; do
   awk '{ print $'$i' }' mois.txt2 | tr "o" "-" > jours$i.txt
done
echo; echo -e "\t\t`cal $A $B | head -1`"
paste jours*.txt
Scrot attached.

Ok. Not exactly on thread. But I found this experiment interesting,
because it enables to treat a month as a DB, in columns, by day of week.
With the default cal, one cannot, because the single-digits days are not
zero-padded.

Once we have the columns (in files jours1.txt, jours2.txt, etc.), we can do
what we want with them, even recreate a pseudo-cal with the paste utility!
(See scrot.)

Or you can type < more jours5.txt >, say, to view all dates for the
Thursdays of the month, etc.

I thought I'd share this with you.

BFN.
More-Uselessness.jpg
 Description   A little time machine!!!
 Filesize   145.67 KB
 Viewed   288 Time(s)

More-Uselessness.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 
musher0

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

PostPosted: Thu 29 Nov 2018, 21:14    Post subject:  

Hello all.

Have you heard of dateutils? I thought it might be useful to
include them in the context of this thread.
Quote:
"Dateutils are a bunch of tools that revolve around fiddling with dates
and times in the command line with a strong focus on use cases that
arise when dealing with large amounts of financial data."
The set contains the following utilities:
    strptime -- Command line version of the C function
    dateadd -- Add durations to dates or times
    dateconv -- Convert dates or times between calendars
    datediff -- Compute durations between dates or times
    dategrep -- Grep dates or times in input streams
    dateround -- Round dates or times to “fuller” values
    dateseq -- Generate sequences of dates or times
    datesort -- Sort chronologically.
    datetest -- Compare dates or times
    datezone -- Convert date/times to timezones in bulk
Further down the page, the reviewer provides how-to's.

Some may find handy the peculiar locale support this set of utilities provides:
Quote:
(...) locale support does not depend on system infrastructure (libc in this
case) nor does it follow the usual semantics of setting LC_TIME, so
different locales can be used for input and output independent of the
user’s environment.


All tools support the --from-locale parameter while tools that output date/
times also support --locale. (...)
(At bottom of presentation page. Underlining by me.)

Source code is here

Compilation on xenialPup-706 & tree of resulting pet archive are attached.

Misc.: Dir. /usr/share/doc/dateutils adds the txt version of the Change
Log found on the utilities' home page, plus files Issues.URL and
Compilation_Formula, not originally in source package.

File Issues.URL contains the report for < make test > and the hyperlink
to dateutils' known issues page.

Enjoy.
dateutils.tree.zip
Description 
zip

 Download 
Filename  dateutils.tree.zip 
Filesize  524 Bytes 
Downloaded  34 Time(s) 
dateutils-4.6.pet
Description 
pet

 Download 
Filename  dateutils-4.6.pet 
Filesize  219.13 KB 
Downloaded  65 Time(s) 

_________________
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: 1842
Location: Japan

PostPosted: Tue 04 Dec 2018, 02:14    Post subject:  

puppy_apprentice wrote:
Nothing is stupid if you can learn from it.
So true Laughing
One thing I learned is that it can be useful when cal displays multiple color marked days and not just a single day.
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.

What I also learned is that grep is pleasantly easy to use but that it is inflexible when it comes to determining the mark color. It's red.
There are ways to change this globally, but why bother? Using sed gives not only full control over the color, it also allows alternative markings, not just ANSI C escape codes. This will become important when writing cal output to GUI dialogs like yad or gtkdialog. These dialogs would know nothing about ANSI C, they would require Pango markup.

The following demo is portable (see shebang) and works for $day being a single day or multiple days. For latter case a regex pattern is required. Separating days with '|' is probably the easiest way to define a range of days.
Code:
#!/bin/busybox ash
day='6|24|25'
month=12
year=2018
cal $month $year | grep -EwC8 --color=always "$day"

BLD=1  #bold: 0=off 1=on
FGD=37 #foreground: 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
BGD=45 #background: 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
cal $month $year | sed -r "s/\b($day)\b/\x1B[$BLD;$FGD;${BGD}m\1\x1B[0m/g"
multiple_days.png
 Description   
 Filesize   4.97 KB
 Viewed   212 Time(s)

multiple_days.png

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

Joined: 21 Aug 2011
Posts: 74

PostPosted: Tue 04 Dec 2018, 16:58    Post subject:  

MochiMoppel wrote:
The following demo is portable (see shebang) and works for $day being a single day or multiple days.


Very good! But doesn't work with busybox grep and sed.


grep --color --> unrecognized option '--color=always'

\x1b --> same as the literal string "x1b"

\b --> works on the PC, not on the phone. Probably because of the bionic C library.



This is the modification that works on my phone too:

Code:
#!/bin/busybox sh

days=" 5|25|26|30|31"
on=$'\x1b'"[1;37;42m"
off=$'\x1b'"[0m"

cal "$@" | sed -r 's/('"$days"')( |$)/'"$on\1$off"'\2/g'




And this is an attempt to combine the two exercises:


Code:
#!/bin/busybox sh

BB=busybox

days=" 5|25|26|30|31"
on=$'\x1b'"[1;37;42m"
off=$'\x1b'"[0m"


$BB cal "$@" | $BB sed -nr '
    1 p; 2 s/(..).(.{17})/\2 \1/p
    3 { s/.*/                  &/; s/ {21}// }
    3,$ { $!N; s/\n/ /g; s/.{21}/&\n/
          h; s/\n.*//; s/('"$days"')( |$)/'"$on\1$off"'\2/g; p; g; D 
        }
'



BTW busybox paste supports custom delimiters but not the empty one. I've invented a square wheel to test your script. It's awkward but doesn't require temporary files.

Code:
BODY=$(busybox awk -v col1="$REST" -v col2="$SUND" 'BEGIN { n = split(col1,a1,"\n"); split(col2,a2,"\n"); for (i = 1; i <= n; i++) print a1[i] a2[i] }')
Back to top
View user's profile Send private message 
musher0

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

PostPosted: Tue 04 Dec 2018, 22:51    Post subject:  

Hello Burunduk.

I just did a < busybox --list > on my xenialPup, and it does not have a
< busybox awk >. Version is BusyBox v1.24.2 (2016-05-06). The xenialPup
has gawk by default, though.

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 
MochiMoppel


Joined: 26 Jan 2011
Posts: 1842
Location: Japan

PostPosted: Tue 04 Dec 2018, 22:55    Post subject:  

Burunduk wrote:
Very good! But doesn't work with busybox grep and sed.
It wasn't meant to be *that* portable Laughing
But of course I like the idea to make it busybox only.

Quote:
This is the modification that works on my phone too:
Are you sure? Does not work when the year is 2018 and day is the 18th Wink

Here is my take:
Code:
#!/bin/busybox ash
days=" 1| 2|1[0-9]"
on=$'\x1b[1;37;42m'
off=$'\x1b[0m'
busybox cal  8 2018 | busybox sed -r "s/(^| )($days)( |$)/\1$on\2$off\3/g; s/(^| )($days)( |$)/\1$on\2$off\3/g"
May look odd. Unlike the GNU version busybox sed would not match consecutive dates and skips every 2nd match. Works when sed repeats the substitution process. Don't know how to fix this more elegantly.
[Edit]Found it. Basically your version. Just excludes 1st line from pattern matching:
Code:
busybox sed -r "1!s/($days)( |$)/$on\1$off\2/g"


Quote:
And this is an attempt to combine the two exercises:
First I need a strong coffee for that ....

Quote:
BTW busybox paste supports custom delimiters but not the empty one. I've invented a square wheel to test your script. It's awkward but doesn't require temporary files.
Wrong thread. Please post to cal command alternatives
BTW Neither busybox awk nor busybox paste exist in my Slacko version.
portable_cal.png
 Description   
 Filesize   2.31 KB
 Viewed   122 Time(s)

portable_cal.png


Last edited by MochiMoppel on Wed 05 Dec 2018, 08:55; edited 1 time in total
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 6 of 7 [97 Posts]   Goto page: Previous 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.0661s ][ Queries: 13 (0.0113s) ][ GZIP on ]