$30 off During Our Annual Pro Sale. View Details »

RE: AWK

wtnabe
February 15, 2014

RE: AWK

Introduction of Regular Expression and AWK on my own

wtnabe

February 15, 2014
Tweet

More Decks by wtnabe

Other Decks in Programming

Transcript

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

    View Slide

  2. Abstract

    View Slide

  3. Regular Expression Basics
    AWK Basics

    View Slide

  4. Regular Expression Basics

    View Slide

  5. Difficulties of RE
    Many different implementations
    Complex Character Sequences

    View Slide

  6. Rough Classification
    POSIX ( Basic / Extended )
    PCRE (Perl Compatible Regular
    Expression)
    PHP, Apache, GNU Grep, ...
    GNU ( Basic / Extended )
    and more

    View Slide

  7. Ignore
    Minor tool's original expressions
    What does grep in your tools mean ?

    View Slide

  8. Basic Syntax
    Literal Character
    Meta Character / Escape Sequence
    Character List / Character Class
    Grouping and Back reference

    View Slide

  9. Elementary Operators
    .
    * + ?
    |
    () grouping and back reference

    View Slide

  10. Elementary Operators
    ^ $ ( \A \z )
    \r \n \s \xXX escape sequence
    [] [^] character list

    View Slide

  11. RE Literal and Language
    Syntax
    Such as escape sequence CONFLICT
    with parent language
    Some Languages have RE Literal
    AWK, Perl, Ruby, JavaScript, ...

    View Slide

  12. Examples
    /^[0-9]+(-[0-9]+)+$/
    /\s[Kk]anazawa\.rb\s/
    %r{\bhttps?://.*\b}

    View Slide

  13. Keep away
    Too Match
    /[0-9-]+/
    Complex one-shot match
    q{[^"'<>]*(?
    :"[^"]*"[^"'<>]*|'[^']*'[^"'<>]*)*(?
    :>|(?=<)|$(?!\n))}; #'}}}}
    cf. Perlメモ

    View Slide

  14. AWK

    View Slide

  15. Name
    from famous Human Names
    Aho
    Weinberger
    Kernighan

    View Slide

  16. Filter-oriented
    Programming Language
    $ awk 'script' srcfile
    $ cat srcfile | awk 'script' > destfile
    $ awk -f script srcfile

    View Slide

  17. Basic Syntax
    C-like / Shell-like ( semicolon less )
    Patterns and Actions
    No need to write about stdin and split

    View Slide

  18. Patterns and Actions
    pattern {
    action
    }
    pattern {
    action
    }

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  22. BEGIN { counting = 0 }
    /^$/ { counting = 0 }
    counting == 1 && !/:/ {
    print $1
    }
    /^GEM$/ { counting = 1 }

    View Slide

  23. $ awk -f script.awk Gemfile.lock | \
    sort | uniq | wc -l

    View Slide

  24. Same as
    NF == 2 && $2 ~ /\(.*\)/ {print $1}

    View Slide

  25. Notes
    http://www.regular-expressions.info/
    正規表現メモ
    The GNU Awk User's Guide
    The GAWK Manual - Table of Contents

    View Slide