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

RE: AWK

088b1b43ff5dd64aa0f000da9e9da777?s=47 wtnabe
February 15, 2014

RE: AWK

Introduction of Regular Expression and AWK on my own

088b1b43ff5dd64aa0f000da9e9da777?s=128

wtnabe

February 15, 2014
Tweet

Transcript

  1. RE: AWK @wtnabe Kanazawa.rb meetup #18 2014-02-15 (Sat) at IT

    Plaza MUSASHI
  2. Abstract

  3. Regular Expression Basics AWK Basics

  4. Regular Expression Basics

  5. Difficulties of RE Many different implementations Complex Character Sequences

  6. Rough Classification POSIX ( Basic / Extended ) PCRE (Perl

    Compatible Regular Expression) PHP, Apache, GNU Grep, ... GNU ( Basic / Extended ) and more
  7. Ignore Minor tool's original expressions What does grep in your

    tools mean ?
  8. Basic Syntax Literal Character Meta Character / Escape Sequence Character

    List / Character Class Grouping and Back reference
  9. Elementary Operators . * + ? | () grouping and

    back reference
  10. Elementary Operators ^ $ ( \A \z ) \r \n

    \s \xXX escape sequence [] [^] character list
  11. RE Literal and Language Syntax Such as escape sequence CONFLICT

    with parent language Some Languages have RE Literal AWK, Perl, Ruby, JavaScript, ...
  12. Examples /^[0-9]+(-[0-9]+)+$/ /\s[Kk]anazawa\.rb\s/ %r{\bhttps?://.*\b}

  13. Keep away Too Match /[0-9-]+/ Complex one-shot match q{[^"'<>]*(? :"[^"]*"[^"'<>]*|'[^']*'[^"'<>]*)*(?

    :>|(?=<)|$(?!\n))}; #'}}}} cf. Perlメモ
  14. AWK

  15. Name from famous Human Names Aho Weinberger Kernighan

  16. Filter-oriented Programming Language $ awk 'script' srcfile $ cat srcfile

    | awk 'script' > destfile $ awk -f script srcfile
  17. Basic Syntax C-like / Shell-like ( semicolon less ) Patterns

    and Actions No need to write about stdin and split
  18. Patterns and Actions pattern { action } pattern { action

    }
  19. BEGIN and END rule BEGIN { ... } END {

    ... }
  20. Example Count a number of gems depending on from Gemfile.lock

  21. Gemfile.lock GEM remote: https://rubygems.org/ specs: blankslate (2.1.2.4) ... PLATFORMS ruby

    DEPENDENCIES ...
  22. BEGIN { counting = 0 } /^$/ { counting =

    0 } counting == 1 && !/:/ { print $1 } /^GEM$/ { counting = 1 }
  23. $ awk -f script.awk Gemfile.lock | \ sort | uniq

    | wc -l
  24. Same as NF == 2 && $2 ~ /\(.*\)/ {print

    $1}
  25. Notes http://www.regular-expressions.info/ 正規表現メモ The GNU Awk User's Guide The GAWK

    Manual - Table of Contents