Slide 1

Slide 1 text

perl for shell, awk and sed programmers YAPC::Hakodate 2024 @mackee_w(macopy)

Slide 2

Slide 2 text

͜ͷτʔΫͰओு͍ͨ͜͠ͱ •օ͞ΜʹPerlΛॻ͍ͯ΋Β͍͍ͨ!!! • ͔͠͠ैདྷͷ࢖͍ํ(WebͳͲ)Ͱ͸ଞͷϓϩάϥϛϯάݴޠͰॻ͍͍ͯΔ ͜ͱͰ͠ΐ͏… • ͦ͜Ͱ ֤छshell, awk, sedΛ࢖͍ͬͯΔ৔໘Ͱ perlΛ࢖ͬͯΈͯ͸Ͳ͏͔ͱ͍͏ఏҊͰ͢

Slide 3

Slide 3 text

͜ͷτʔΫͰ࿩͢͜ͱ • Ͳ͏͍͏৔໘Ͱshell,awk,sed͕࢖ΘΕ͍ͯΔ͔ • shell,awk,sedͦΕͧΕͷϝϦοτɾσϝϦοτ • perlͷ͜ΕΒʹର͢Δ༗རͳ఺ • ͍ͭshell,awk,sedΛ࢖͏΂͖͔ɾ͍ͭperlΛ࢖͏΂͖͔

Slide 4

Slide 4 text

͜ͷτʔΫͰ࿩͞ͳ͍͜ͱ • PerlҎ֎ͷεΫϦϓτݴޠͱͷൺֱ • ΋͠Ӊ஦ਓ͕߈ΊࠐΜͰ͖ͯϦϑΝϨϯεΧ΢ϯτҎ֎ͷGCΛਓྨʹېࢭͨ࣌͠ͷ͜ͱΛߟ͍͑ͯͩ͘͞… • खʹೃછΉεΫϦϓτݴޠ͕͋Ε͹ͦͪΒΛ࢖͏ͷ΋ྑ͍Ͱ͠ΐ͏ • γΣϧ͸ةͳ͍ʂͱ͔awk͸ෆศʂͱ͔ͦ͏͍͏ݴઆ • ͜͏͍͏έʔε͸ؒҧ͍΍͍͢ɾඇޓ׵͕͋Δ͸ݴ͏͚ΕͲਓ͕ؒ ؾΛ͚ͭΕ͹શ෦࢖͑·͢

Slide 5

Slide 5 text

஻Δਓ • X: @mackee_w • GitHub: @mackee • PAUSE: MACOPY • ໘ന๏ਓΧϠοΫ • SRE/αʔόʔαΠυΤϯδχΞ

Slide 6

Slide 6 text

[AD] ίʔυΰϧϑίϯςετPerlbatross΍ͬͯ·͢

Slide 7

Slide 7 text

஫ऍ: `perl`ͱ`Perl`ͷҧ͍ • ͜ͷεϥΠυͰ͸`perl`ͱ`Perl`ΛҎԼͷΑ͏ʹ࢖͍෼͚·͢ • `perl`: perlίϚϯυͷ͜ͱ ྫ: ʮperlίϚϯυͷҾ਺͸ʙʯ • `Perl`: ϓϩάϥϛϯάݴޠͷ͜ͱ ྫ: ʮPerlͷҰจࣈม਺͸ʙʯ • ରԠ • bash, zsh, awk, sed => perl • γΣϧεΫϦϓτ, awkεΫϦϓτ => Perl

Slide 8

Slide 8 text

sed • ඪ४ೖྗͷςΩετΛҰఆͷϧʔϧͰஔ׵͠ग़ྗ͢ΔίϚϯυ • ਖ਼نදݱͰͷஔ׵ʹಛԽͨ͠ίϚϯυͱݴ͑Δ • จࣈྻஔ׵Λ͍ͨ͠৔߹ʹศརʂ

Slide 9

Slide 9 text

Α͘΍Δ΍ͭ snake case/camel caseม׵

Slide 10

Slide 10 text

Α͘΍Δ΍ͭ snake case/camel caseม׵ gsedͬͯԿʁ

Slide 11

Slide 11 text

!CAUTION!: sedʹ͸ෳ਺ͷ࣮૷͕͋Γ·͢ • BSD sedͱGNU sed • macOSʹ͸BSD sed͕ೖ͍ͬͯΔ • ֦ுਖ਼نදݱ͸ͲͪΒ΋ -r ͔ -E Ͱ༗ޮʹͳΔ • খจࣈେจࣈม׵(\l\u\L\U)͸GNU sedͷ֦ு • BSD sedͰ-iΦϓγϣϯΛҾ਺ແ͠Ͱ࢖͑ͳ͍

Slide 12

Slide 12 text

!CAUTION!: sedʹ͸ෳ਺ͷ࣮૷͕͋Γ·͢ ྫ͑͹ -i ͷڍಈ͕ҧ͏

Slide 13

Slide 13 text

ղܾํ๏ • ࣮ߦଆͷsedΛݻఆ͢Δ • macOSͰGNU sedΛೖΕΔͳͲ • ͔͠͠aliasΛషͬͨΓgsedͱ࣮ߦ͢Δ͔Ͳ͏͔ΛܾΊΔϥούʔε ΫϦϓτ͕ඞཁʹ • ͜ͷτʔΫͰͷఏҊ: perlΛ࢖͏ͷ͸Ͳ͏͔

Slide 14

Slide 14 text

perl͸perl͔͠ͳ͍

Slide 15

Slide 15 text

awk • ඪ४ೖྗ͔ΒϑΟϧλ΍ม׵Λߦͳͬͯඪ४ग़ྗʹग़͢ίϚϯυ • ͦͷม׵΍ϑΟϧλͷࢦࣔΛawkεΫϦϓτͱݺͿ • ࣮͸΋ͬͱڧྗͳػೳ͕͋ΓɺͦΕ͸YAPC::Hiroshima 2024ͷτʔ ΫʮawkͰͭͬͯ͘Θ͔ΔɺWebΞϓϦέʔγϣϯʯ΍ʮϓϩάϥϛ ϯάݴޠAWKʯΛಡΈ·͠ΐ͏

Slide 16

Slide 16 text

awkͷ࢖༻ྫ ߦࢦ޲σʔλͷॲཧʹϐολϦ

Slide 17

Slide 17 text

awkͷಘҙͳ͜ͱ΍࢖͍ಓ • ߦ͝ͱʹεϖʔε΍ΧϯϚ۠੾Γͷೖྗ(CSVͳͲ)Λ౷ܭॲཧ͢Δ • ਺ߦͣͭ΍ɺϑΟʔϧυʹର͢Δਖ਼نදݱͳͲͷύλʔϯΛ؆୯ʹ ͔͚Δ • ෳ਺ͷϑΟʔϧυʹ෼ׂͨ͠ޙʹਖ਼نදݱΛؚΜͩݕࡧΛ͢Δ৔߹΍ ϑΟʔϧυͰ؆୯ͳܭࢉ΍ूܭΛ͢Δ৔߹ʹڧ͍ • sed΍grepͳͲʹൺ΂Δͱͱ͍͏࿩

Slide 18

Slide 18 text

!CAUTION! : awkʹ͸ෳ਺ͷ࣮૷͕͋Γ·͢ʂ • macOSʹೖ͍ͬͯΔnawkͱLinuxͳͲͷgawk, mawk, BusyBox awk • Ұ෦ͷม਺ɾؔ਺͕gawkҎ֎Ͱ͸࢖͑ͳ͍ • systime, strftimeͳͲͷ࣌ؒؔ܎ͷؔ਺͸gawkͷΈ • matchͷޙํࢀরΛม਺ʹ୅ೖ͢Δetc… • ύλʔϯϚονͰ΋ࠩҟ͕͋Δ

Slide 19

Slide 19 text

ύλʔϯϚονͰͷࠩҟ

Slide 20

Slide 20 text

gawkͷΈͰಈ͘ྫ

Slide 21

Slide 21 text

awkͰ͸ಈ͍͍ͯͳ͍

Slide 22

Slide 22 text

ղܾࡦ • ར༻ऀଆͷawkΛݻఆ͢Δ • macOSͷํ͸brewͰgawkΛೖΕ·͠ΐ͏Ͷͱ͔ • ͔ͦ͠͠ͷ৔߹͸ϥούʔεΫϦϓτͰ෼ذ͕ඞཁʹͳΓ… • ͜ͷτʔΫͷఏҊ: perlͰಉ༷ͷ͜ͱΛ΍Δͷ͸Ͳ͏͔ʁ

Slide 23

Slide 23 text

AWKʹ޲͔ͳ͍ॲཧ΋͋Δ • awk͸૊ΈࠐΈؔ਺͕গͳ͘ɺ·ͨϞδϡʔϧͷγεςϜ͸ͳ͍ɻ • ୯ͳΔߦ୯Ґͷϧʔϓɺ࢛ଇԋࢉɺਖ਼نදݱʹΑΔՃ޻Ҏ্ͱͳΔͱ͔ͳΓݫ ͍͠ • JSONͷύʔεͱ͔ • ʮϓϩάϥϛϯάݴޠawkୈ2൛ʯʹ͸awkεΫϦϓτ͔ΒCݴޠͷίʔυ ʹτϥϯεύΠϧ͢Δawkύʔαʔͷίʔυ͕awkͰॻ͔Ε͍ͯΔ(!) • ࣮༻͢Δʹ͸͍ͩͿݫ͍͠(ओ؍ͷҙݟͰ͢)

Slide 24

Slide 24 text

perlʹ͸awkͬΆ͍Ϟʔυ͕͋Δ

Slide 25

Slide 25 text

perlϫϯϥΠφʔͰ΍Δ͍͍ͱ͜Ζ • ਖ਼نදݱपΓ͕͞Βʹڧྗ • nawkͩͱޙํࢀরͷѻ͍͕೉͍͠(RSTART,RLENGTH) • PerlͳΒevalͯؔ͠਺࢖ͬͯՃ޻·ͰͰ͖ͯ͠·͏ • JSON΍Β΋ѻ͑ΔΑʂ • jqΛ࢖ͬͯϑΟϧλ͢Δέʔε΋࣮͸perlϫϯϥΠφʔͰͰ͖·͢ • ෳࡶͳߏ଄Λѻ͑Δ ϋογϡͳͲ

Slide 26

Slide 26 text

Ұํawkͷྑ͍ͱ͜Ζ • ߦ୯ҐɾϑΟʔϧυ୯ҐͷॲཧΛ؆୯ʹॻ͘ʹ͸ൺֱత୹͘ॻ͚Δ • બ୒తʹผϑΝΠϧ΁ग़ྗ͢ΔͳͲ΋Ͱ͖Δ • Perl͸ϑΝΠϧΛಡΈॻ͖͢Δͷ͸(ൺֱత)ख͕͔͔ؒΔ

Slide 27

Slide 27 text

shell • ͜͜Ͱ͸bash, zshͳͲͷPOSIXγΣϧ࣮૷ͷ૯শͱ͠·͢ • օ͞Μ͕Linux΍BSD, macOS্ͰίϚϯυΛଧͭ࣌ʹ࢖͏΍ͭ • ٞ࿦͍ͨ͠ͷ͸shellΛλʔήοτͱͯ͠༻͍ͯهड़ɾ࣮ߦ͢ΔγΣϧ εΫϦϓτ

Slide 28

Slide 28 text

ྫ: IPv6ͱIPv4Ͱͷifcon fi g.meͷग़ྗͷҧ͍Λgist΁

Slide 29

Slide 29 text

γΣϧεΫϦϓτ͸άϧʔݴޠͰ͋Δ • άϧʔݴޠ: ίϯϙʔωϯτಉ࢜Λ݁ͼ͚ͭΔ͜ͱΛओ؟ͱͨ͠ݴޠ • ԿΛ͚ͬͭ͘Δͷ͔ʁ: ίϚϯυͱϑΝΠϧ • ૉͰॻ͍ͨ΋ͷ͸ίϚϯυ࣮ߦʹͳΔ • ଞͷϓϩάϥϛϯάݴޠʹ͸ͳ͍ಛ௃ • ϦμΠϨΫτͳͲϑΝΠϧͷಡΈॻ͖ʹศརͳػೳ͕͋Δ • ͦ΋ͦ΋UNIX͕ϑΝΠϧૢ࡞Λओମͱͨ͠OSͰ͋Δ

Slide 30

Slide 30 text

shell͸ͲΜͳ؀ڥͰ΋ೖ͍ͬͯΔ…ͱ͞Ε͍ͯΔʁ • ʢͱ͍͏લఏΛ͓͍ͯྑ͍ͱ͞Ε͍ͯΔʣʢཁग़యʣ • Πϯετʔϧͷ͜ͱΛߟ͑ͳͯ͘ྑ͍͠ɺҰ౓ॻ͚͹ͲΜͳ؀ڥͰ΋ ಈ͘ͱ͞Ε͍ͯΔ • ྫ͑͹։ൃऀͷϩʔΧϧͰಈ͘εΫϦϓτͳͲ͸ಈ࡞؀ڥΛ౷ҰͰ ͖ͳͯ͘΋γΣϧεΫϦϓτ͸ಈ͘Մೳੑ͕ߴ͍ • ͱ͞Ε͍ͯΔ……ʁ • ͦ͏Ͱ΋ͳͦ͞͏

Slide 31

Slide 31 text

!CAUTION!: γΣϧεΫϦϓτͷ࣮૷͕ෳ਺͋Δ • ݱ୅Ͱ޿͘࢖ΘΕ͍ͯΔ΋ͷͩͱBusyBox ash, dash, bash, zsh etc… • େମbash͕ͩ…macOSͰ͸zsh, BusyBoxͰ͸ash,Debian౳Ͱ͸ dash • ͦΕͧΕPOSIXγΣϧʹՃ͑ͯಠࣗͷ֦ுػೳ͕͋Δ

Slide 32

Slide 32 text

ઌͷγΣϧεΫϦϓτ͕ଞͷ࣮૷Ͱಈ͔ͳ͍ྫ

Slide 33

Slide 33 text

bash֦ுͷศརͳػೳ • ϓϩηεஔ׵ • γΣϧม਺ͷஔ׵ • ifͳͲͷࣜධՁ [[ … ]]

Slide 34

Slide 34 text

ղܾࡦ • POSIXγΣϧͱͯ͠ͷػೳͷΈͰॻ͘ • ࣮ߦଆͷγΣϧΛݻఆ͢Δ • shebangʹ/bin/bashͱॻ͚͹ྑ͍ • ͕ɺAlpineLinux౳Ͱ௥ՃͰೖΕͳ͍ͱ͍͚ͳ͘ͳΔ • ͜͜ͰͷఏҊ: PerlͰॻ͍ͯperlͰಈ͔ͯ͠Έͯ͸Ͳ͏͔ʁ

Slide 35

Slide 35 text

γΣϧεΫϦϓτ͸ؒҧ͑΍͍͢ϙΠϯτ͕͋Δ • վߦ΍εϖʔεͷ͋Δͳ͕͔͠ͳΓݫີ͔ͭɺҧ͏ҙຯʹͳΔ͜ͱ͕ ͋Δ • ม਺ͷείʔϓ͕μΠφϛοΫείʔϓ • ୯ޠ෼ׂɾύε໊ల։

Slide 36

Slide 36 text

γΣϧεΫϦϓτ͸ؒҧ͑΍͍͢ϙΠϯτ͕͋Δ

Slide 37

Slide 37 text

ղܾࡦ • shellcheckΛ࢖͍·͠ΐ͏ 👉 • γΣϧεΫϦϓτ޲͚ͷLinter • ςετΛॻ͘ • Bats-core, ShellSpec • ͜͜ͰͷఏҊ: PerlͰॻ͍ͯperl Ͱಈ͔͢ͷ͸Ͳ͏͔ʁ

Slide 38

Slide 38 text

Perl/perlͷ͍͍ͱ͜Ζ • perlͱ͍͑͹ͨͩҰͭͷ࣮૷Λࢦ͢ ͦͯ͠ѹ౗తͳޙํޓ׵ੑ • (POSIXγΣϧͱൺ΂ͯ)ਐԽ͠ଓ͚Δݴޠػೳ • Track B 14:50ʙʮྩ࿨࠷৽൛PerlίʔσΟϯάΨΠυʯΛฉ͘ͱྑͦ͞͏ • ঎༻WebΞϓϦ։ൃ͕޿͘Ͱ͖Δఔ౓ʹ·ͱ΋ͳϓϩάϥϛϯάݴޠ • ‘use strict;’ ͳͲͷΨʔυϨʔϧ΋͋Δఔ౓૊ΈࠐΈଘࡏ͢Δ • ίΞϞδϡʔϧͰͰ͖Δ͜ͱ͕γΣϧεΫϦϓτ+POSIXίϚϯυΑΓ͸ଟ͍ • ͍͟ͱͳΕ͹CPANϞδϡʔϧͰػೳ֦ுͰ͖Δ γΣϧ΋bpkgͱ͍͏ͷ͕͋Γ·͢ • ෳࡶͳσʔλߏ଄Λѻ͑Δ

Slide 39

Slide 39 text

Perl/perlͷͭΒ͍ͱ͜Ζ • ಡΈॻ͖Ͱ͖Δਓ͕গͳ͘ͳ͍ͬͯΔ • ͜ͷτʔΫͷཪͷ໨త͸ࢲ͕࢓ࣄͰPerlεΫϦϓτΛॻ͍ͯ΋ڻ͔Εͳ͍͜ͱͰ͢ • ୯ͳΔίϚϯυ࣮ߦ΍ϑΝΠϧͷಡΈॻ͖͸γΣϧεΫϦϓτͷํ͕؆୯ʹॻ͚ΔՄೳੑ ͕ߴ͍ • Perlͷ৔߹֎෦ίϚϯυ࣮ߦ͸system(...), `…`, qx(…), ͳͲͱॻ͔ͳ͍ͱ͍͚ͳ͍ • ͜ͷ৔߹ίϚϯυΑΓ͸ϞδϡʔϧΛ࢖ͬͨ΄͏͕Perlͷར఺͕ग़Δ • ࣮ߦʹඞཁͳϑΝΠϧαΠζ(perlίϚϯυ)͕େ͖͍ • ͜Εʹ͍ͭͯ͸ޙ೔ղܾࡦΛϒϩάʹॻ͖·͢

Slide 40

Slide 40 text

Perlͱͦͷଞͷ࢖͍෼͚ • ΋͋͠ͳ͕ͨ΍Γ͍ͨ͜ͱ͕ߦࢦ޲σʔλͷγϯϓϧͳϑΟϧλɾม׵͔ͭɺෳ ਺ͷϑΝΠϧʹు͖͍ͨͷͰ͋Ε͹ • awkͰ΍Δͱ୹͙͘͢ॻ͚ΔՄೳੑ͕ߴ͍ • ΋͋͠ͳ͕ͨ΍Γ͍ͨ͜ͱ͕ίϚϯυ࣮ߦ΍ϑΝΠϧૢ࡞͕ओମͰ͋Ε͹ • γΣϧεΫϦϓτͷํ͕޾ͤʹͳΔՄೳੑ͕ߴ͍ • ΍Γ͍ͨ͜ͱ͕ෳࡶͳσʔλͷม׵ɾՃ޻ɺߏ଄ԽσʔλͷύʔεͰ͋Ε͹ • PerlͰ΍Δํ͕޾ͤʹͳΔՄೳੑ͕ߴ͍

Slide 41

Slide 41 text

͜Εෳࡶͩͳʁ ޓ׵ੑΛߟྀ͠ͳͪ͘ΌͱࢥͬͨΒ PerlͰॻ͘͜ͱ΋ݕ౼ͯ͠Έ͍ͯͩ͘͞

Slide 42

Slide 42 text

ࢀߟจݙ • ࡾ୐ल໌.৽͍͠γΣϧϓϩάϥϛϯάͷڭՊॻ.SBΫϦΤΠςΟϒ,2017. • Alfred V, Aho; Brian W, Kernighan; Peter J, Weinberger; ઌॅ ࣏࿠ ༁.ϓϩάϥϛϯάݴޠ AWK ୈೋ൛.ΦϥΠ Ϧʔδϟύϯ,2024 • εΫϦϓτݴޠͱͯ͠Έ֤ͨPOSIXγΣϧͷಛ௃ͱޓ׵ੑ্ͷ஫ҙ఺·ͱΊ https://qiita.com/ko1nksm/ items/8d28d4f7cb2c325c00fa • sed(1) - FreeBSD Manual Pages https://man.freebsd.org/cgi/man.cgi?sed(1) • GNU sed https://www.gnu.org/software/sed/ • AWKॲཧܥͷൺֱ http://www.yosbits.com/wordpress/?p=2702 • ༣ศ൪߸σʔλμ΢ϯϩʔυ https://www.post.japanpost.jp/zipcode/download.html

Slide 43

Slide 43 text

૝ఆ࣭໰ • ͦͷଞඇޓ׵͕͋ΔίϚϯυ make, grep ͷ৔߹͸ʁ • Ͳ͏͢Ε͹͍͍ΜͰ͠ΐ͏Ͷ͑ʁɹͲ͏ͯ͠·͢ʁɹmakeʹؔͯ͠͸npm run script΍rakeͳͲͷݴޠσϑΝΫτͷ΍Γ ํͰஔ͖׵͑ͨํ͕͍͍ؾ͕͢Δͱ͍͏ͷ͕࠷ۙͷߟ͑Ͱ͢ • ޷͖ͳperlͷΦϓγϣϯΛڭ͑ͯʂ • `-E` Ͱ͢ɻsayͳͲͷϞμϯͳػೳ͕Ұؾʹ༗ޮʹͳΔ͔Β • ීஈͷ฻Β͠Ͱ͸Perl/perlΛͲ͏͍͏;͏ʹ࢖ͬͯ·͔͢ • Α͘࢖͏ͷ͸ `rg ./ -l | xargs perl -pi -E ’s/…/…/‘` • base64/url encodeͷม׵ͳΜ͔΋perlίϚϯυͰ΍ͬͯ·͢ • JSONܗࣜͷϩάϑΝΠϧ͔ΒಡΈऔͬͯίϚϯυΛෳ਺ݸ࣮ߦ͢ΔͷʹPerlεΫϦϓτͰίϚϯυΛྻڍ͢ΔγΣϧε ΫϦϓτΛग़ྗͯ͠ɺҰԠਓͷ໨Ͱݟ͔ͯΒ࣮ߦͨ͜͠ͱ͕͋Δ

Slide 44

Slide 44 text

ݕূʹ࢖༻ͨ͠όʔδϣϯ • sed (macOS Sonomaͷ΋ͷ) • gsed (GNU sed) 4.9 • awk version 20200816 (nawk) • GNU awk 5.3.1, API 4.0, (GNU MPFR 4.2.1, GNU MP 6.3.0) • shellʹ͍ͭͯ͸ϖʔδ27ʹهࡌ • This is perl 5, version 34, subversion 0 (v5.34.0) built for darwin-thread- multi-2level