90 likes | 203 Views
CS311 – Lecture 04 Outline. Bash scripting Understand the script2 script Understand the myWC script grep - find pattern matches REG ular EX pressions sed : stream editing
E N D
CS311 – Lecture 04 Outline • Bash scripting • Understand the script2 script • Understand the myWC script • grep - find pattern matches • REGular EXpressions • sed: stream editing Note: These lecture notes are not intended replace your notes. This is what I expect your notes to already look like, but with more explanation as needed. CS311 – Operating Systems 1 1
Bash scripting language #! /bin/sh shebang lines ' vs " vs \ quoting ` ` command substitution $# bash variable for how many command line args were given $1 bash variable that retrieves the 1st command line arg. $USER environmental variable, your username exit bash builtin for exiting bash CS311 – Operating Systems 1 2
Flow control statements Lecture 02 • if statements • use test utility to test for equality or < or > or if a file exists. • for loops CS311 – Operating Systems 1 3 3
grep - find pattern matches Grep filters lines of text based on patterns grep [options] pattern [file] This util allows a filename. But, of course, you can send it input through pipes or <. CS311 – Operating Systems 1 4
REGular EXpressions Regular expressions are character sequences that describe a family of matching strings every letter matches itself ^ - start of line, or negation of characters (if it is in []) $ - end of line . - any character [] – range, or set of characters * - any number of times + - at least once ? - exactly once CS311 – Operating Systems 1 5
REGular EXpressions echo "cowboys" | grep ".*" echo "cowboys" | grep ". . . . . . ." echo "cowboys" | grep "c.*s" echo "cowgirls" | grep "c.*s" echo "tomboys" | grep "c.*" echo "aaabb" | grep "^a*b*$" echo "bbbb" | grep "^a*b*$" echo "ccbbb" | grep "^a*b*$" echo "aabbb" | grep "^a+b*$" echo "bbb" | grep "^a+b*$" yes yes yes yes no yes yes no yes no CS311 – Operating Systems 1 6
REGular EXpressions echo "cowboy" | grep "^[a-z]*$" echo "cowboy123" | grep "^[a-z]*$" echo "cowboy123" | grep "^[a-z]*[0-9]*$" echo "cowboy" | grep "^[bcowy]*$" yes no yes yes Lecture 04 CS311 – Operating Systems 1 7 7
grep - find pattern matches • Useful options • -E • -c • -o • -n • -i CS311 – Operating Systems 1 8
sed -r "s/regex/text/g" filename substitute every occurrence of the regular expressions regex by the string text. CS311 – Operating Systems 1 9