Portál AbcLinuxu, 11. května 2025 00:34

Dotaz: BASH - potrebujem ziskat week of the month do premenej

cbrpnk avatar 23.4.2022 22:28 cbrpnk | skóre: 8 | blog: bl0gium
BASH - potrebujem ziskat week of the month do premenej
Přečteno: 711×
Odpovědět | Admin
Zdravim, v BASHi - potrebujem ziskat week of the month do premenej (1 az 4 aktualneho mesiaca). Ako na to?

Řešení dotazu:


Nástroje: Začni sledovat (1) ?Zašle upozornění na váš email při vložení nového komentáře.

Odpovědi

Jendа avatar 23.4.2022 22:59 Jendа | skóre: 78 | blog: Jenda | JO70FB
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
Odpovědět | | Sbalit | Link | Blokovat | Admin
No v nejhorším iterovat od jedničky po aktuální den a počítat neděle (nebo jak je definováno že začal týden). Přes date --date 2021-04-23 +%u
24.4.2022 00:24 z_sk | skóre: 34 | blog: analyzy
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
Odpovědět | | Sbalit | Link | Blokovat | Admin
Vyuzi 2 vlastnosti:
debian.plus@protonmail.com
24.4.2022 00:54 jejda | skóre: 25 | blog: jejda
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
Odpovědět | | Sbalit | Link | Blokovat | Admin
Napadlo mě vyparsovat výstup programu cal.
cal | tail -n6 | grep -nw $(date +%e) | cut -d: -f1
Akorát to teda nejsou týdny 1-4 ale 1-6, protože týden má jenom 7 dní a né každý měsíc začíná v pondělí.
24.4.2022 01:27 z_sk | skóre: 34 | blog: analyzy
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
Asi zle, lebo Ti týždeň začína v nedeľu, a nie v pondelok.
debian.plus@protonmail.com
k3dAR avatar 24.4.2022 01:50 k3dAR | skóre: 63
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
tak "ncal -1 -b -M" by mel od pondelka, ale spis mi unika proc:
"cal | grep 24" vrati prazdno a ne radek s dnem 24 :-)
porad nemam telo, ale uz mam hlavu... nobody
k3dAR avatar 24.4.2022 01:52 k3dAR | skóre: 63
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
aha, ridici znaky pro tucne, takze jeste pridat parametr "neotucnuj dnesni den" :-)
ncal -1 -b -M -h | tail -n6 | grep -nw $(date +%e) | cut -d: -f1
porad nemam telo, ale uz mam hlavu... nobody
k3dAR avatar 24.4.2022 01:56 k3dAR | skóre: 63
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
btw: ja chtel puvodne nadhodit "$cislo_tydne_dnes - $cislo_tydne_1_v_tomto_mesici" ale nedoslo mi ze prvni dny v roku (pokud 1.1. neni pondeli), maji cislo tydne bud 52 (+%V) nebo 00 (+%W), takze by se muselo to jeste doosetrit pro leden :-)
porad nemam telo, ale uz mam hlavu... nobody
24.4.2022 01:59 jejda | skóre: 25 | blog: jejda
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
No to záleží od toho, jakou máš locale LC_TIME. Jestli máš americkou en_US tak ano, v americe začíná týden v neděli. Já používám en_GB protože mě to americké formátování připadá úchylné ale zároveň chcu systém v angličtině. Jestli má locale česky, slovensky nebo en_GB tak mu týden začíná v pondělí.
k3dAR avatar 24.4.2022 03:45 k3dAR | skóre: 63
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
a aktualni den ti grepnout jde?
porad nemam telo, ale uz mam hlavu... nobody
24.4.2022 04:27 jejda | skóre: 25 | blog: jejda
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
Nějak nechápu na co se ptáš. cal vypíše toto:
cal
     April 2022
Mo Tu We Th Fr Sa Su
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

cal | grep -wo $(date +%e)
24
cal | tail -n6 | grep -wn $(date +%e)
4:18 19 20 21 22 23 24
Nevím ale jaký je rozdíl mezi cal a ncal. Ten druhý nemám vůbec nainstalovaný.
Jendа avatar 24.4.2022 06:15 Jendа | skóre: 78 | blog: Jenda | JO70FB
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
V Debianu implementace cal nedávno nějak měnili, možná máte jinou verzi / jiné alternatives.
k3dAR avatar 24.4.2022 13:22 k3dAR | skóre: 63
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
v Xubuntu 20.04 "cal" zobrazuje zvyraznene aktualni den ;-)
ls -l $(which cal)
lrwxrwxrwx 1 root root 4 bře 30  2020 /usr/bin/cal -> ncal

dpkg -S bin/cal | grep 'cal$'
bsdmainutils: /usr/bin/cal

dpkg -l | grep bsdmainutils
ii  bsdmainutils    11.1.2ubuntu3    amd64    collection of more utilities from FreeBSD
man cal
CAL(1)                                                                                                 BSD General Commands Manual                                                                                                CAL(1)

NAME
     cal, ncal — displays a calendar and the date of Easter

SYNOPSIS
     cal [-31jy] [-A number] [-B number] [-d yyyy-mm] [[month] year]
     cal [-31j] [-A number] [-B number] [-d yyyy-mm] -m month [year]
     ncal [-C] [-31jy] [-A number] [-B number] [-d yyyy-mm] [[month] year]
     ncal [-C] [-31j] [-A number] [-B number] [-d yyyy-mm] -m month [year]
     ncal [-31bhjJpwySM] [-A number] [-B number] [-H yyyy-mm-dd] [-d yyyy-mm] [-s country_code] [[month] year]
     ncal [-31bhJeoSM] [-A number] [-B number] [-d yyyy-mm] [year]

DESCRIPTION
     The cal utility displays a simple calendar in traditional format and ncal offers an alternative layout, more options and the date of Easter.  The new format is a little cramped but it makes a year fit on a 25x80 terminal.  If
     arguments are not specified, the current month is displayed.

     The options are as follows:

     -h      Turns off highlighting of today.

     -J      Display Julian Calendar, if combined with the -o option, display date of Orthodox Easter according to the Julian Calendar.

     -e      Display date of Easter (for western churches).

     -j      Display Julian days (days one-based, numbered from January 1).

     -m month
             Display the specified month.  If month is specified as a decimal number, appending ‘f’ or ‘p’ displays the same month of the following or previous year respectively.

     -o      Display date of Orthodox Easter (Greek and Russian Orthodox Churches).

     -p      Print the country codes and switching days from Julian to Gregorian Calendar as they are assumed by ncal.  The country code as determined from the local environment is marked with an asterisk.

     -s country_code
             Assume the switch from Julian to Gregorian Calendar at the date associated with the country_code.  If not specified, ncal tries to guess the switch date from the local environment or falls back to September 2, 1752.
             This was when Great Britain and her colonies switched to the Gregorian Calendar.

     -w      Print the number of the week below each week column.

     -y      Display a calendar for the specified year. This option is implied when a year but no month are specified on the command line.

     -3      Display the previous, current and next month surrounding today.

     -1      Display only the current month. This is the default.

     -A number
             Months to add after. The specified number of months is added to the end of the display. This is in addition to any date range selected by the -y, -3, or -1 options. For example, “cal -y -B2 -A2” shows everything from
             November of the previous year to February of the following year. Negative numbers are allowed, in which case the specified number of months is subtracted. For example, “cal -y -B-6” shows July to December. And “cal
             -A11” simply shows the next 12 months.

     -B number
             Months to add before. The specified number of months is added to the beginning of the display. See -A for examples.

     -C      Completely switch to cal mode. For cal like output only, use -b instead.

     -N      Switch to ncal mode.

     -d yyyy-mm
             Use yyyy-mm as the current date (for debugging of date selection).

     -H yyyy-mm-dd
             Use yyyy-mm-dd as the current date (for debugging of highlighting).

     -M      Weeks start on Monday.

     -S      Weeks start on Sunday.

     -b      Use oldstyle format for ncal output.

     A single parameter specifies the year (1–9999) to be displayed; note the year must be fully specified: “cal 89” will not display a calendar for 1989.  Two parameters denote the month and year; the month is either a number be‐
     tween 1 and 12, or a full or abbreviated name as specified by the current locale.  Month and year default to those of the current system clock and time zone (so “cal -m 8” will display a calendar for the month of August in the
     current year).

     Not all options can be used together. For example, the options -y, -3, and -1 are mutually exclusive. If inconsistent options are given, the later ones take precedence over the earlier ones.

     A year starts on January 1.

     Highlighting of dates is disabled if stdout is not a tty.

SEE ALSO
     calendar(3), strftime(3)

HISTORY
     A cal command appeared in Version 5 AT&T UNIX.  The ncal command appeared in FreeBSD 2.2.6.  The output of the cal command is supposed to be bit for bit compatible to the original Unix cal command, because its output is pro‐
     cessed by other programs like CGI scripts, that should not be broken. Therefore it will always output 8 lines, even if only 7 contain data. This extra blank line also appears with the original cal command, at least on Solaris 8

AUTHORS
     The ncal command and manual were written by Wolfgang Helbig <helbig@FreeBSD.org>.

BUGS
     The assignment of Julian–Gregorian switching dates to country codes is historically naive for many countries.

     Not all options are compatible and using them in different orders will give varying results.

BSD                                                                                                          March 14, 2009                                                                                                          BSD
porad nemam telo, ale uz mam hlavu... nobody
24.4.2022 15:07 jejda | skóre: 25 | blog: jejda
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
Příloha:
Aha, archlinux má teda jinačí cal.
cal -V
cal from util-linux 2.38
Zvláštní je, že ten aktuální den mi to zvýrazňuje taky (otočí barvy), ale grepnout to jde bez problému.
Jendа avatar 24.4.2022 06:13 Jendа | skóre: 78 | blog: Jenda | JO70FB
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
Jestli chceš rozumné formátování třeba v ls (protože jinak to vypisuje někdy den a čas pro nedávné soubory, pak měsíc a den pro soubory asi tak do roka a kdovíco pro starší) a tak, tak
export TIME_STYLE="+%Y-%m-%d %H:%M:%S"
chvíli jsem používal nějaké en_DK locales kvůli tomu, ale mělo to nějaké jiné problémy. Obecně najít rozumné nastavení pro čas, čísla (desetinná tečka), papír (A4) a řazení (collate) je quest který jsem ještě nedokončil, aktuálně používám LC_COLLATE a LC_MESSAGES C, zbytek cs_CZ. Ale některé aplikace to stejně ignorují.
24.4.2022 16:13 jejda | skóre: 25 | blog: jejda
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
Jojo TIME_STYLE používám. A řazení českých znaků s diakritikou mi záhadně funguje i s anglickým en_GB locales.
locale
LANG=en_GB.UTF-8
LC_CTYPE="en_GB.UTF-8"
LC_NUMERIC="en_GB.UTF-8"
LC_TIME="en_GB.UTF-8"
LC_COLLATE="en_GB.UTF-8"
LC_MONETARY="en_GB.UTF-8"
LC_MESSAGES="en_GB.UTF-8"
LC_PAPER="en_GB.UTF-8"
LC_NAME="en_GB.UTF-8"
LC_ADDRESS="en_GB.UTF-8"
LC_TELEPHONE="en_GB.UTF-8"
LC_MEASUREMENT="en_GB.UTF-8"
LC_IDENTIFICATION="en_GB.UTF-8"
LC_ALL=

touch 0 1 2 3 a A b B c C č Č d D ď Ď e E ě Ě é É f F

ls -l
total 0
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 0
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 1
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 2
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 3
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 a
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 A
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 b
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 B
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 c
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 C
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 č
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 Č
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 d
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 D
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 ď
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 Ď
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 e
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 E
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 é
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 É
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 ě
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 Ě
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 f
-rw-r--r-- 1 bb users 0 2022-04-24 13:46:48 F

TIME_STYLE=locale ls -l
total 0
-rw-r--r-- 1 bb users 0 Apr 24 13:46 0
-rw-r--r-- 1 bb users 0 Apr 24 13:46 1
-rw-r--r-- 1 bb users 0 Apr 24 13:46 2
-rw-r--r-- 1 bb users 0 Apr 24 13:46 3
-rw-r--r-- 1 bb users 0 Apr 24 13:46 a
-rw-r--r-- 1 bb users 0 Apr 24 13:46 A
-rw-r--r-- 1 bb users 0 Apr 24 13:46 b
-rw-r--r-- 1 bb users 0 Apr 24 13:46 B
-rw-r--r-- 1 bb users 0 Apr 24 13:46 c
-rw-r--r-- 1 bb users 0 Apr 24 13:46 C
-rw-r--r-- 1 bb users 0 Apr 24 13:46 č
-rw-r--r-- 1 bb users 0 Apr 24 13:46 Č
-rw-r--r-- 1 bb users 0 Apr 24 13:46 d
-rw-r--r-- 1 bb users 0 Apr 24 13:46 D
-rw-r--r-- 1 bb users 0 Apr 24 13:46 ď
-rw-r--r-- 1 bb users 0 Apr 24 13:46 Ď
-rw-r--r-- 1 bb users 0 Apr 24 13:46 e
-rw-r--r-- 1 bb users 0 Apr 24 13:46 E
-rw-r--r-- 1 bb users 0 Apr 24 13:46 é
-rw-r--r-- 1 bb users 0 Apr 24 13:46 É
-rw-r--r-- 1 bb users 0 Apr 24 13:46 ě
-rw-r--r-- 1 bb users 0 Apr 24 13:46 Ě
-rw-r--r-- 1 bb users 0 Apr 24 13:46 f
-rw-r--r-- 1 bb users 0 Apr 24 13:46 F
Den má hezky 24 hodin a né 12 AM PM chaos jak v americe. Datum to má den/měsíc/rok a ne měsíc/den/rok a podobné zvěrstva. Akorát koš v thunaru se nejmenuje trash ale Wastebasket :-D
xxxs avatar 24.4.2022 14:58 xxxs | skóre: 25 | blog: vetvicky
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
Odpovědět | | Sbalit | Link | Blokovat | Admin
prvy tyzden pocitas podla konvencie, kde je prvy stvrtok, alebo podla toho, kde je prvy den v mesiaci?

toto dava aktualny week, podel si mesiacmi:

date +%W
xxxs avatar 24.4.2022 15:01 xxxs | skóre: 25 | blog: vetvicky
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
ooops. tak nepodel...
xxxs avatar 24.4.2022 15:06 xxxs | skóre: 25 | blog: vetvicky
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
mozes delit 4 a podla zvysku sa ti opakuje.
24.4.2022 21:29 Aleš Kapica | skóre: 52 | blog: kenyho_stesky | Ostrava
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
Odpovědět | | Sbalit | Link | Blokovat | Admin

Sice si to nezasloužíš. Ale budiž…

~$ DATUM=2022-02-24
~$ DATUM=$((($(date +%-d --date=$DATUM)-1)/7+1))
~$ echo $DATUM
~$ 4
xkucf03 avatar 24.4.2022 22:55 xkucf03 | skóre: 49 | blog: xkucf03
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
Tam bude potřeba zakomponovat ještě aktuální den v týdnu, ne?
$ den_v_mesici() { echo $((($(date +%-d --date=$1)-1)/7+1)); }

$ for ((i=1;i<=31;i++)); do datum="2022-04-$i"; echo -n "$datum: "; den_v_mesici $datum; done                                                                                                                                     
2022-04-1: 1                                                                                                                                                                                                                                 
2022-04-2: 1                                                                                                                                                                                                                                 
2022-04-3: 1                                                                                                                                                                                                                                 
2022-04-4: 1                                                                                                                                                                                                                                 
2022-04-5: 1                                                                                                                                                                                                                                 
2022-04-6: 1                                                                                                                                                                                                                                 
2022-04-7: 1
2022-04-8: 2
2022-04-9: 2
2022-04-10: 2
2022-04-11: 2
2022-04-12: 2
2022-04-13: 2
2022-04-14: 2
2022-04-15: 3
2022-04-16: 3
2022-04-17: 3
2022-04-18: 3
2022-04-19: 3
2022-04-20: 3
2022-04-21: 3
2022-04-22: 4
2022-04-23: 4
2022-04-24: 4
2022-04-25: 4
2022-04-26: 4
2022-04-27: 4
2022-04-28: 4
2022-04-29: 5
2022-04-30: 5
2022-04-31: date: chybné datum: „2022-04-31“
1
Mám rád, když se lidé přou, znamená to, že vědí, co dělají, a že mají směr. Frantovo.cz, SQL-DK, Relational pipes
24.4.2022 23:30 Want
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
Nevím co má za data, a co s nimi chce dělat. Navíc mi přijde tenhle požadavek poněkud ošemetný. Zrovna ten duben. Začínal v pátek. Co je první týden v měsíci? Týden na který připadnul ten víkend? Nebo týden, na který připadlo první pondělí?

Prostě základ má a dál už si to musí přiohnout podle sebe. RTFM.
Řešení 1× (Gréta)
26.4.2022 22:08 Andrej | skóre: 51 | blog: Republic of Mordor
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
Odpovědět | | Sbalit | Link | Blokovat | Admin

Zaprvé, je to 1 až 6, nikoliv 1 až 4.

Zadruhé, už zase se to tady hemží všemi možnými programy, jenom ne Bashem. :-(

Zatřetí, úkol se dá chápat dvěma různými způsoby:

  1. počet celých uplynulých týdnů od začátku měsíce plus 1, tedy například pokud měsíc začal středou, následující neděle, pondělí i úterý dají výsledek 1 a po nich následující středa dá výsledek 2. Tohle bude v rozsahu 1 až 5.
  2. počet týdnů začínajících pondělkem, které se (dosud) překrývaly s tímto měsícem, tedy například pokud měsíc začal středou, následující neděle dá (ještě) výsledek 1, zatímco po ní následující pondělí a úterý už dají výsledek 2. Tohle bude v rozsahu 1 až 6.

První interpretace úkolu (v Bashi, nikoliv v pěti jiných programech!):

declare -i week_of_the_month
week_of_the_month="$(((10#$(printf '%(%d)T' "$((EPOCHSECONDS))") + 6) / 7))"

Druhá interpretace úkolu (opět v Bashi):

week_of_the_month() {
  local -i epoch {current,first}_week day_offset
  epoch=EPOCHSECONDS
  current_week=$((10#$(printf '%(%W)T' "$((epoch))")))
  day_offset=$((10#$(printf '%(%d)T' "$((epoch))") - 1))
  first_week=$((10#$(printf '%(%W)T' "$((epoch - day_offset * 86400))")))
  echo "$((current_week - first_week + 1))"
}

declare -i week_of_the_month
week_of_the_month="$(week_of_the_month)"

Tak. A teď to celé o-unit-testujeme, tentokrát už za použití nástrojů mimo Bash (například date):

(
week_of_the_month() {
  local -i epoch {current,first}_week day_offset
  epoch=EPOCHSECONDS
  current_week=$((10#$(printf '%(%W)T' "$((epoch))")))
  day_offset=$((10#$(printf '%(%d)T' "$((epoch))") - 1))
  first_week=$((10#$(printf '%(%W)T' "$((epoch - day_offset * 86400))")))
  echo "$((current_week - first_week + 1))"
}

unset EPOCHSECONDS                                # ztrácí zvláštní význam!
declare -i EPOCHSECONDS                           # poor man’s mock
EPOCHSECONDS="$(date '+%s' -d 2021-12-31T12:00)"  # Silvestr 2021, poledne

for ((i = 0; i < 365; ++i)); do
  ((EPOCHSECONDS += 86400))                       # plus 1 den
  echo -n "$(date '+%F' -d "@${EPOCHSECONDS}"): možnost 1: "
  echo "$(((10#$(printf '%(%d)T' "$((EPOCHSECONDS))") + 6) / 7))" 
  echo -n "$(date '+%F' -d "@${EPOCHSECONDS}"): možnost 2: "
  week_of_the_month
  echo
done
)

Za povšimnutí stojí výsledek 6 u 2. možnosti a dnů 31. ledna, 30. a 31. května a 31. října. Kdepak 1 až 4!

26.4.2022 22:53 Aleš Kapica | skóre: 52 | blog: kenyho_stesky | Ostrava
Rozbalit Rozbalit vše Re: BASH - potrebujem ziskat week of the month do premenej
Jsi z zvrhlý Andreji, víš o tom? ;-) Proč mu blbneš hlavu, když ani nevíš co je pro něj začátkem týdne ;-)

Založit nové vláknoNahoru

Tiskni Sdílej: Linkuj Jaggni to Vybrali.sme.sk Google Del.icio.us Facebook

ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.