Upgrade to Pro — share decks privately, control downloads, hide ads and more …

perl for shell, awk and sed programmers

mackee
October 04, 2024

perl for shell, awk and sed programmers

YAPC::Hakodate 2024 Track A 12:25〜

mackee

October 04, 2024
Tweet

More Decks by mackee

Other Decks in Programming

Transcript

  1. ஻Δਓ • X: @mackee_w • GitHub: @mackee • PAUSE: MACOPY

    • ໘ന๏ਓΧϠοΫ • SRE/αʔόʔαΠυΤϯδχΞ
  2. ஫ऍ: `perl`ͱ`Perl`ͷҧ͍ • ͜ͷεϥΠυͰ͸`perl`ͱ`Perl`ΛҎԼͷΑ͏ʹ࢖͍෼͚·͢ • `perl`: perlίϚϯυͷ͜ͱ ྫ: ʮperlίϚϯυͷҾ਺͸ʙʯ •

    `Perl`: ϓϩάϥϛϯάݴޠͷ͜ͱ ྫ: ʮPerlͷҰจࣈม਺͸ʙʯ • ରԠ • bash, zsh, awk, sed => perl • γΣϧεΫϦϓτ, awkεΫϦϓτ => Perl
  3. !CAUTION!: sedʹ͸ෳ਺ͷ࣮૷͕͋Γ·͢ • BSD sedͱGNU sed • macOSʹ͸BSD sed͕ೖ͍ͬͯΔ •

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

    • systime, strftimeͳͲͷ࣌ؒؔ܎ͷؔ਺͸gawkͷΈ • matchͷޙํࢀরΛม਺ʹ୅ೖ͢Δetc… • ύλʔϯϚονͰ΋ࠩҟ͕͋Δ
  5. γΣϧεΫϦϓτ͸άϧʔݴޠͰ͋Δ • άϧʔݴޠ: ίϯϙʔωϯτಉ࢜Λ݁ͼ͚ͭΔ͜ͱΛओ؟ͱͨ͠ݴޠ • ԿΛ͚ͬͭ͘Δͷ͔ʁ: ίϚϯυͱϑΝΠϧ • ૉͰॻ͍ͨ΋ͷ͸ίϚϯυ࣮ߦʹͳΔ •

    ଞͷϓϩάϥϛϯάݴޠʹ͸ͳ͍ಛ௃ • ϦμΠϨΫτͳͲϑΝΠϧͷಡΈॻ͖ʹศརͳػೳ͕͋Δ • ͦ΋ͦ΋UNIX͕ϑΝΠϧૢ࡞Λओମͱͨ͠OSͰ͋Δ
  6. !CAUTION!: γΣϧεΫϦϓτͷ࣮૷͕ෳ਺͋Δ • ݱ୅Ͱ޿͘࢖ΘΕ͍ͯΔ΋ͷͩͱBusyBox ash, dash, bash, zsh etc… •

    େମbash͕ͩ…macOSͰ͸zsh, BusyBoxͰ͸ash,Debian౳Ͱ͸ dash • ͦΕͧΕPOSIXγΣϧʹՃ͑ͯಠࣗͷ֦ுػೳ͕͋Δ
  7. Perl/perlͷ͍͍ͱ͜Ζ • perlͱ͍͑͹ͨͩҰͭͷ࣮૷Λࢦ͢ ͦͯ͠ѹ౗తͳޙํޓ׵ੑ • (POSIXγΣϧͱൺ΂ͯ)ਐԽ͠ଓ͚Δݴޠػೳ • Track B 14:50ʙʮྩ࿨࠷৽൛PerlίʔσΟϯάΨΠυʯΛฉ͘ͱྑͦ͞͏

    • ঎༻WebΞϓϦ։ൃ͕޿͘Ͱ͖Δఔ౓ʹ·ͱ΋ͳϓϩάϥϛϯάݴޠ • ‘use strict;’ ͳͲͷΨʔυϨʔϧ΋͋Δఔ౓૊ΈࠐΈଘࡏ͢Δ • ίΞϞδϡʔϧͰͰ͖Δ͜ͱ͕γΣϧεΫϦϓτ+POSIXίϚϯυΑΓ͸ଟ͍ • ͍͟ͱͳΕ͹CPANϞδϡʔϧͰػೳ֦ுͰ͖Δ γΣϧ΋bpkgͱ͍͏ͷ͕͋Γ·͢ • ෳࡶͳσʔλߏ଄Λѻ͑Δ
  8. Perl/perlͷͭΒ͍ͱ͜Ζ • ಡΈॻ͖Ͱ͖Δਓ͕গͳ͘ͳ͍ͬͯΔ • ͜ͷτʔΫͷཪͷ໨త͸ࢲ͕࢓ࣄͰPerlεΫϦϓτΛॻ͍ͯ΋ڻ͔Εͳ͍͜ͱͰ͢ • ୯ͳΔίϚϯυ࣮ߦ΍ϑΝΠϧͷಡΈॻ͖͸γΣϧεΫϦϓτͷํ͕؆୯ʹॻ͚ΔՄೳੑ ͕ߴ͍ • Perlͷ৔߹֎෦ίϚϯυ࣮ߦ͸system(...),

    `…`, qx(…), ͳͲͱॻ͔ͳ͍ͱ͍͚ͳ͍ • ͜ͷ৔߹ίϚϯυΑΓ͸ϞδϡʔϧΛ࢖ͬͨ΄͏͕Perlͷར఺͕ग़Δ • ࣮ߦʹඞཁͳϑΝΠϧαΠζ(perlίϚϯυ)͕େ͖͍ • ͜Εʹ͍ͭͯ͸ޙ೔ղܾࡦΛϒϩάʹॻ͖·͢
  9. ࢀߟจݙ • ࡾ୐ल໌.৽͍͠γΣϧϓϩάϥϛϯάͷڭՊॻ.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
  10. ૝ఆ࣭໰ • ͦͷଞඇޓ׵͕͋ΔίϚϯυ make, grep ͷ৔߹͸ʁ • Ͳ͏͢Ε͹͍͍ΜͰ͠ΐ͏Ͷ͑ʁɹͲ͏ͯ͠·͢ʁɹmakeʹؔͯ͠͸npm run script΍rakeͳͲͷݴޠσϑΝΫτͷ΍Γ

    ํͰஔ͖׵͑ͨํ͕͍͍ؾ͕͢Δͱ͍͏ͷ͕࠷ۙͷߟ͑Ͱ͢ • ޷͖ͳperlͷΦϓγϣϯΛڭ͑ͯʂ • `-E` Ͱ͢ɻsayͳͲͷϞμϯͳػೳ͕Ұؾʹ༗ޮʹͳΔ͔Β • ීஈͷ฻Β͠Ͱ͸Perl/perlΛͲ͏͍͏;͏ʹ࢖ͬͯ·͔͢ • Α͘࢖͏ͷ͸ `rg <searchword> ./ -l | xargs perl -pi -E ’s/…/…/‘` • base64/url encodeͷม׵ͳΜ͔΋perlίϚϯυͰ΍ͬͯ·͢ • JSONܗࣜͷϩάϑΝΠϧ͔ΒಡΈऔͬͯίϚϯυΛෳ਺ݸ࣮ߦ͢ΔͷʹPerlεΫϦϓτͰίϚϯυΛྻڍ͢ΔγΣϧε ΫϦϓτΛग़ྗͯ͠ɺҰԠਓͷ໨Ͱݟ͔ͯΒ࣮ߦͨ͜͠ͱ͕͋Δ
  11. ݕূʹ࢖༻ͨ͠όʔδϣϯ • 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