advanced unix
play

Advanced UNIX CIS 218 Advanced UNIX Regular Expressions See also: - PowerPoint PPT Presentation

Advanced UNIX CIS 218 Advanced UNIX Regular Expressions See also: www.regex101.com www.regexr.com 1 CIS 218 Advanced UNIX Why Regular Expressions? To locate text To change text To delineate metacharacters from ordinary characters


  1. Advanced UNIX CIS 218 Advanced UNIX Regular Expressions See also: www.regex101.com www.regexr.com 1 CIS 218 Advanced UNIX

  2. Why Regular Expressions?  To locate text  To change text  To delineate metacharacters from ordinary characters  To suppress evaluation of metacharacters by the shell  Different from filename expansion rules 2 CIS 218 Advanced UNIX

  3. vi Commands using Strings  /text search forward for text  :s/old/new/g replace every occurrence of old by new fc replaced by  :1,.s/fc/function/g function between line 1 and current 3 CIS 218 Advanced UNIX

  4. Strings Examples ring, spring , ringing /ring/ Thursday, Thurs day’s /Thurs/ /or not/ poor nothing 4 CIS 218 Advanced UNIX

  5. Regular Expressions (REs)  A RE is a string with special characters that defines one or more strings.  Special characters in vi : . [...] (with - and ^) * ^ and $ \ 5 CIS 218 Advanced UNIX

  6. ‘.’ Special Character  matches any single character RE Examples /.ing/ singing, ping will talk, may balk / .alk/ 6 CIS 218 Advanced UNIX

  7. ‘[...]’ Special Characters  Match any single character given inside the brackets: i.e. [aeiou] is any single vowel – ‘ - ’ to specify a range – ‘^’ to make the range negative (this meaning for ‘^’ only applies inside [...])  ‘ \ ’, ‘*’, ‘$’ loose their special character meaning 7 CIS 218 Advanced UNIX

  8. RE Examples bill , Bill , billed /[bB]ill/ talkative, stink , /t[aeiou].k/ teak , tanker number 6 0, number 8:, /number [6-9]/ get number 9 1, 7, @, ., } , Stop ! /[^a-zA-Z]/ 8 CIS 218 Advanced UNIX

  9. ‘*’ Special Character  Match 0 or more occurrences of a character RE Examples ac , abc , abbc, debbcaabbbc /ab*c/ abc , abxc , ab45c, /ab.*c/ xab 756.345 x cat 1. any string without nums /[a-zA-Z ]*/ or punctuation ! 9 CIS 218 Advanced UNIX

  10. Longest Match Possible RE Examples /(.*)/ Get (this) and (that); /([^)]*)/ Get (this) and (that); /s.*ing/ singing songs, singing more 10 CIS 218 Advanced UNIX

  11. ‘^’ and ‘$’ Special Characters  ‘^’ matches a string at the beginning of a line  ‘$’ matches a string at the end of a line 11 CIS 218 Advanced UNIX

  12. RE Examples /^T/ This line..., That Time..., In Time /^+[0 - 9]/ +5 +45.72, +759 Keep this... ...below : /:$/ ...:+++: 12 CIS 218 Advanced UNIX

  13. ‘ \ ’ Special Character  ‘ \ ’ can be used to quote a special character to make it represent itself: \\ \* \. etc. 13 CIS 218 Advanced UNIX

  14. RE Examples The end. , send. , /end\./ /\\/ \ an asterisk ( * ) /\*/ it was five [5] /\[5\]/ /and\/or/ and/or 14 CIS 218 Advanced UNIX

  15. Use of REs in grep  Put RE in single quotes ‘...’: $ grep ‘st.ing’ file $ grep ‘ooo*’ file $ grep ‘^T’ file $ grep ‘foo[0 - 9]’ file 15 CIS 218 Advanced UNIX

  16. Full (Extended) Regular Expressions  Default on most current UNIX versions  Extended form of RE used by e grep (and some other commands) The additional special characters:  + ? |  Can use ‘+’, ‘?’, and ‘*’ with parentheses (...) 16 CIS 218 Advanced UNIX

  17. ‘+’ Special Character  Matches 1 or more occurrences of a character RE Examples yabcw, abbc 57 ‘ab+c’ zabcd, ababc ! ‘(ab)+c’  longest match possible rule applies! 17 CIS 218 Advanced UNIX

  18. ‘?’ Special Character  Matches 0 or 1 occurrences of a character RE Examples ‘ab?c’ back, abcdef xc , abcc ‘(ab)?c/ 18 CIS 218 Advanced UNIX

  19. ‘|’ Special Character  means ‘or’; used between two REs RE Examples ‘ab|ac’ ab , ac , abac ‘^Exit|^Quit’ Exit ..., Quit..., No Exit P.D. Jones , N. Jones ‘(D|N) \ . Jones’ 19 CIS 218 Advanced UNIX

  20. RegEx Examples cats: cat cattle catalog scrawny cat vacation wildcat  (each on a separate line)  grep ca cats  grep cat cats  grep cat? cats  grep cat. cats  grep a cats  grep -v tt fruits  grep ^c cats  grep ‘t$' cats  grep '^' cats   20 CIS 218 Advanced UNIX

  21. RegEx Examples Fruits: apple orange pear peach grape banana blueberry plum  (each on a separate line) grep pear fruits  grep ea fruits  grep a fruits  grep -v a fruits  grep ^p fruits.txt  grep 'e$' fruits.txt  grep '^' fruits.txt  21 CIS 218 Advanced UNIX

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend