bashscript to count the number of occurences of friday the 13th

For discussions about programming and projects not necessarily associated with Porteus.
Kulle
Warlord
Warlord
Posts: 602
Joined: 28 Jan 2017, 10:39
Distribution: v4.0 64bit Xfce
Location: Berlin

bashscript to count the number of occurences of friday the 13th

Post#1 by Kulle » 17 Mar 2024, 17:47

I found this bash script:
https://www.linuxquestions.org/question ... 175503292/
The script Friday.sh is single-line:

Code: Select all

#!/bin/bash
cal -y $1|awk '{print $6$13$20}'|grep -o 13|wc -l
But script Friday.sh gives incorrect results:

Code: Select all

guest@porteux:~$ ./Friday.sh 2024
3
guest@porteux:~$ ./Friday.sh 2023
1
Can the script be corrected?

User avatar
M. Eerie
Moderator
Moderator
Posts: 622
Joined: 31 Aug 2017, 21:18
Distribution: Nemesis Xfce/MATE x64

bashscript to count the number of occurences of friday the 13th

Post#2 by M. Eerie » 17 Mar 2024, 18:12

Works for me as it is.

Probably your config is different (the week start day is on Monday instead of Sunday).

$6$13$20 are the columns corresponding to each Friday for each quarter as shown in the console. Simply change the numbers to the appropriate column:
$5$12$19 maybe.
> Does not compute_ 🖖

https://forum.porteus.org/viewtopic.php?p=94310#p94310
https://forum.porteus.org/viewtopic.php?p=84002#p84002
https://forum.porteus.org/viewtopic.php?p=77174#p77174
https://forum.porteus.org/viewtopic.php?f=39&t=8584

User avatar
Ed_P
Contributor
Contributor
Posts: 8374
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

bashscript to count the number of occurences of friday the 13th

Post#3 by Ed_P » 18 Mar 2024, 04:07

Kulle wrote:
17 Mar 2024, 17:47
The script Friday.sh is single-line:

Code: Select all

#!/bin/bash
cal -y $1|awk '{print $6$13$20}'|grep -o 13|wc -l
:o An impressive line of code. :)
Ed

Kulle
Warlord
Warlord
Posts: 602
Joined: 28 Jan 2017, 10:39
Distribution: v4.0 64bit Xfce
Location: Berlin

bashscript to count the number of occurences of friday the 13th

Post#4 by Kulle » 18 Mar 2024, 08:33

Hi M. Eerie,
thank you.
I made the change.
Correct results are now displayed for 2024 and 2023.

But for 2015 an incorrect result is shown: 4
That's impossible. A maximum of only 3 is possible.
This can be proven relatively easily mathematically.
So there is still an error in the line of code.
Which ??

Code: Select all

guest@porteux:~$ ./Friday.sh 2024
2
guest@porteux:~$ ./Friday.sh 2023
2
guest@porteux:~$ ./Friday.sh 2015
4
guest@porteux:~$ cal 2015
                               2015                               

       Januar                 Februar                 März        
Mo Di Mi Do Fr Sa So   Mo Di Mi Do Fr Sa So   Mo Di Mi Do Fr Sa So
          1  2  3  4                      1                      1
 5  6  7  8  9 10 11    2  3  4  5  6  7  8    2  3  4  5  6  7  8
12 13 14 15 16 17 18    9 10 11 12 13 14 15    9 10 11 12 13 14 15
19 20 21 22 23 24 25   16 17 18 19 20 21 22   16 17 18 19 20 21 22
26 27 28 29 30 31      23 24 25 26 27 28      23 24 25 26 27 28 29
                                              30 31               
        April                   Mai                   Juni        
Mo Di Mi Do Fr Sa So   Mo Di Mi Do Fr Sa So   Mo Di Mi Do Fr Sa So
       1  2  3  4  5                1  2  3    1  2  3  4  5  6  7
 6  7  8  9 10 11 12    4  5  6  7  8  9 10    8  9 10 11 12 13 14
13 14 15 16 17 18 19   11 12 13 14 15 16 17   15 16 17 18 19 20 21
20 21 22 23 24 25 26   18 19 20 21 22 23 24   22 23 24 25 26 27 28
27 28 29 30            25 26 27 28 29 30 31   29 30               
                                                                  
        Juli                  August                September     
Mo Di Mi Do Fr Sa So   Mo Di Mi Do Fr Sa So   Mo Di Mi Do Fr Sa So
       1  2  3  4  5                   1  2       1  2  3  4  5  6
 6  7  8  9 10 11 12    3  4  5  6  7  8  9    7  8  9 10 11 12 13
13 14 15 16 17 18 19   10 11 12 13 14 15 16   14 15 16 17 18 19 20
20 21 22 23 24 25 26   17 18 19 20 21 22 23   21 22 23 24 25 26 27
27 28 29 30 31         24 25 26 27 28 29 30   28 29 30            
                       31                                         
       Oktober               November               Dezember      
Mo Di Mi Do Fr Sa So   Mo Di Mi Do Fr Sa So   Mo Di Mi Do Fr Sa So
          1  2  3  4                      1       1  2  3  4  5  6
 5  6  7  8  9 10 11    2  3  4  5  6  7  8    7  8  9 10 11 12 13
12 13 14 15 16 17 18    9 10 11 12 13 14 15   14 15 16 17 18 19 20
19 20 21 22 23 24 25   16 17 18 19 20 21 22   21 22 23 24 25 26 27
26 27 28 29 30 31      23 24 25 26 27 28 29   28 29 30 31         
                       30                                      

Kulle
Warlord
Warlord
Posts: 602
Joined: 28 Jan 2017, 10:39
Distribution: v4.0 64bit Xfce
Location: Berlin

bashscript to count the number of occurences of friday the 13th

Post#5 by Kulle » 18 Mar 2024, 09:33

The result 4 is also shown for the years 2009 2012 2015 2026.
Has anyone an idea of ​​the reasons why?

User avatar
M. Eerie
Moderator
Moderator
Posts: 622
Joined: 31 Aug 2017, 21:18
Distribution: Nemesis Xfce/MATE x64

bashscript to count the number of occurences of friday the 13th

Post#6 by M. Eerie » 18 Mar 2024, 11:14

Because awk's default field separator truncates the total number of columns for each record.

Code: Select all

 cal -m -y 2012 | awk -F' ' '{print $5$12$19}' | grep -o 13 | wc -l
4
is different from:

Code: Select all

 cal -m -y 2012 | awk -F' ' '{print $5" "$12" "$19}' | grep -o 13 | wc -l
3
> Does not compute_ 🖖

https://forum.porteus.org/viewtopic.php?p=94310#p94310
https://forum.porteus.org/viewtopic.php?p=84002#p84002
https://forum.porteus.org/viewtopic.php?p=77174#p77174
https://forum.porteus.org/viewtopic.php?f=39&t=8584

Kulle
Warlord
Warlord
Posts: 602
Joined: 28 Jan 2017, 10:39
Distribution: v4.0 64bit Xfce
Location: Berlin

bashscript to count the number of occurences of friday the 13th

Post#7 by Kulle » 18 Mar 2024, 13:23

Thank you M. Eerie,
I wouldn't have found this error for the rest of my life.
How would you know that?!

User avatar
Ed_P
Contributor
Contributor
Posts: 8374
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

bashscript to count the number of occurences of friday the 13th

Post#8 by Ed_P » 18 Mar 2024, 18:47

This dresses it up a bit.

Code: Select all

#!/bin/bash
echo -en "\033]0;Friday 13s in $1\a" && echo
cal -y $1|awk '{print $6" "$13" "$20}'|grep -o 13|wc -l && echo
:D
Ed

User avatar
M. Eerie
Moderator
Moderator
Posts: 622
Joined: 31 Aug 2017, 21:18
Distribution: Nemesis Xfce/MATE x64

bashscript to count the number of occurences of friday the 13th

Post#9 by M. Eerie » 18 Mar 2024, 21:18

Kulle wrote:
18 Mar 2024, 13:23
How would you know that?!
One way to trace the response of a command is to interrogate it in different phases and with different parameters to see its response. In this way,

Code: Select all

awk '{print NR,NF}' german-cal.txt
returns the number of fields (NF) that each record (NR) has. If we look at the first one, we see that there is only one field, when if the separator were a space, there should be more (there are a lot of spaces in front of '2015'). So, we must change the FS (field separator) to achieve our desired result.

* german-cal.txt is nothing but your output saved into that named file.

I am convinced that there is a cleaner way to achieve the same result with awk alone (no need for grep and wc).
> Does not compute_ 🖖

https://forum.porteus.org/viewtopic.php?p=94310#p94310
https://forum.porteus.org/viewtopic.php?p=84002#p84002
https://forum.porteus.org/viewtopic.php?p=77174#p77174
https://forum.porteus.org/viewtopic.php?f=39&t=8584

User avatar
Ed_P
Contributor
Contributor
Posts: 8374
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

bashscript to count the number of occurences of friday the 13th

Post#10 by Ed_P » 18 Mar 2024, 21:48

M. Eerie wrote:
18 Mar 2024, 21:18

Code: Select all

awk '{print NR,NF}' german-cal.txt
returns the number of fields (NF) that each record (NR) has.

Code: Select all

guest@porteus:~$ cal -y 2024 | awk '{print NR,NF}'
1 1
2 0
3 3
4 21
5 11
6 21
7 21
8 21
9 16
10 1
11 3
12 21
13 11
14 21
15 21
16 21
17 16
18 1
19 3
20 21
21 16
22 21
23 21
24 21
25 13
26 0
27 3
28 21
29 14
30 21
31 21  
32 21
33 15
34 0
guest@porteus:~$ 
34! :o Not 52? The weeks in a year.

This is interesting also.

Code: Select all

guest@porteus:~$ cal -y 2024 | awk '{print $6,$13,$20}'|grep -0 13
12 9 13
--
11 8 13
guest@porteus:~$ 
No " "s needed.
Ed

User avatar
M. Eerie
Moderator
Moderator
Posts: 622
Joined: 31 Aug 2017, 21:18
Distribution: Nemesis Xfce/MATE x64

bashscript to count the number of occurences of friday the 13th

Post#11 by M. Eerie » 19 Mar 2024, 16:57

Code: Select all

#!/bin/bash
read -p "Enter the year: " year
cal -m -y $year | awk '{count+= (($5==13) + ($12==13) + ($19==13))} END {print count}'
Only awk ;)
> Does not compute_ 🖖

https://forum.porteus.org/viewtopic.php?p=94310#p94310
https://forum.porteus.org/viewtopic.php?p=84002#p84002
https://forum.porteus.org/viewtopic.php?p=77174#p77174
https://forum.porteus.org/viewtopic.php?f=39&t=8584

User avatar
Ed_P
Contributor
Contributor
Posts: 8374
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

bashscript to count the number of occurences of friday the 13th

Post#12 by Ed_P » 20 Mar 2024, 16:19

M. Eerie wrote:
19 Mar 2024, 16:57
Only awk
:o :lol:
Ed

Kulle
Warlord
Warlord
Posts: 602
Joined: 28 Jan 2017, 10:39
Distribution: v4.0 64bit Xfce
Location: Berlin

bashscript to count the number of occurences of friday the 13th

Post#13 by Kulle » 20 Mar 2024, 18:47

Bash programming is extremely powerful,
but I know very little.
I've never used awk before
AI is often very helpful

Explanation by AI (chatGPT):
This bash script is short and may be difficult to understand for someone unfamiliar with bash scripting. Let's break it down step by step:

#!/bin/bash: This is known as a shebang line and is used to specify the interpreter to be used for the script, in this case, bash.

cal -y $1: This command prints a calendar for the specified year, which is provided as a argument $1 when running the script. The -y option specifies the year.

|: This is called a pipe and is used to pass the output of one command as input to another command.

awk '{print $5" "$12" "$19}': This awk command extracts specific columns from the output of the cal command. It prints the 5th, 12th, and 19th columns separated by spaces.

|grep -o 13: This grep command searches for the number 13 in the output of the previous command. The -o option tells grep to only print the matching parts of a line, in this case, 13.

|wc -l: Finally, the output of the grep command is piped to wc -l, which counts the number of lines in the input. This effectively counts the occurrences of the number 13 in the output of the cal command.

In summary, this script takes a year as an argument, prints a calendar for that year, extracts specific columns, searches for the number 13 in the extracted columns, and counts the number of occurrences of 13. The final result is the number of occurrences of the number 13 in the calendar for the specified year.

Post Reply