Introduction of Regular Expression and AWK on my own
RE: AWK@wtnabeKanazawa.rb meetup #182014-02-15 (Sat) at IT Plaza MUSASHI
View Slide
Abstract
Regular Expression BasicsAWK Basics
Regular Expression Basics
Difficulties of REMany different implementationsComplex Character Sequences
Rough ClassificationPOSIX ( Basic / Extended )PCRE (Perl Compatible RegularExpression)PHP, Apache, GNU Grep, ...GNU ( Basic / Extended )and more
IgnoreMinor tool's original expressionsWhat does grep in your tools mean ?
Basic SyntaxLiteral CharacterMeta Character / Escape SequenceCharacter List / Character ClassGrouping and Back reference
Elementary Operators.* + ?|() grouping and back reference
Elementary Operators^ $ ( \A \z )\r \n \s \xXX escape sequence[] [^] character list
RE Literal and LanguageSyntaxSuch as escape sequence CONFLICTwith parent languageSome Languages have RE LiteralAWK, Perl, Ruby, JavaScript, ...
Examples/^[0-9]+(-[0-9]+)+$//\s[Kk]anazawa\.rb\s/%r{\bhttps?://.*\b}
Keep awayToo Match/[0-9-]+/Complex one-shot matchq{[^"'<>]*(?:"[^"]*"[^"'<>]*|'[^']*'[^"'<>]*)*(?:>|(?=<)|$(?!\n))}; #'}}}}cf. Perlメモ
AWK
Namefrom famous Human NamesAhoWeinbergerKernighan
Filter-orientedProgramming Language$ awk 'script' srcfile$ cat srcfile | awk 'script' > destfile$ awk -f script srcfile
Basic SyntaxC-like / Shell-like ( semicolon less )Patterns and ActionsNo need to write about stdin and split
Patterns and Actionspattern {action}pattern {action}
BEGIN and END ruleBEGIN {...}END {...}
ExampleCount a number of gems depending onfrom Gemfile.lock
Gemfile.lockGEMremote: https://rubygems.org/specs:blankslate (2.1.2.4)...PLATFORMSrubyDEPENDENCIES...
BEGIN { counting = 0 }/^$/ { counting = 0 }counting == 1 && !/:/ {print $1}/^GEM$/ { counting = 1 }
$ awk -f script.awk Gemfile.lock | \sort | uniq | wc -l
Same asNF == 2 && $2 ~ /\(.*\)/ {print $1}
Noteshttp://www.regular-expressions.info/正規表現メモThe GNU Awk User's GuideThe GAWK Manual - Table of Contents