Quotes: single vs. double vs. grave accent % set day = date % echo day % echo $day date % echo '$day' $day % echo "$day" date % echo `$day` Mon Jul 30 17: 32: 35 EDT 2001
vi and ex are basically the same! • Editor was designed to allow input from a line oriented terminal (teletype) • Editor has to be switched between input mode and command mode ESC -- I • Commands appear at bottom of screen after : • For example substitution of text : s/UNIX/Unix/ • • will replace UNIX by Unix in current line In general : s/pattern/text/ Any other separator besides / can also be used
Rules for search pattern used by other commands vi provides the ‘: ’ • • • : s/summer/each &/ – replaces first occurrence of summer in line by each summer : s/pattern/text/g – All occurrences in file are changed : /Unix/s/System/SVR 4/g – Searches for a line containing Unix and in this line replaces all occurrences of System with SVR 4 • : g/Unix/s/System/SVR 4/g – Searches for all lines containing Unix and in these lines replaces all occurrences of System with SVR 4 • : g/Unix/s/System/SVR 4/gp – Will display changes as they are made
Rules for search patterns x matches that character, if x is not special x matches character x, to be used with. * [ ] and ^, can not be used with NEWLINE Special characters. * [ ] – ^ at beginning of string – $ at end of string ^ matches beginning of line $ matches end of line. matches any single character [string] matches any single character in string [x-z] matches any single character from x to z [^string] matches any character not in string pattern* matches zero or more repetitions of pattern < matches beginning of a word > matches end of a word
Examples of search patterns • • • [9] [9] . \\ [A-Z] ^Unix$ [^A-Z] A. C A. *C searches for the number 9 searches for [9] Searches for three dots … Searches for \ Searches for any capital letter Searches for Unix at start of line Searches for Unix at end of line Searches for characters other than capital letters matches Ax. C with x any character matches any string, which has an A and later a C, i. e. AC AXC QASDECS
More examples H. . H followed by any two characters H[a-z] H followed by any two lower case characters ^Harry$ Harry on a single line <now> now as a complete word, surrounded by blanks or tabs • Whitespaces include <space>, <formfeed>, <newline>, <carriage return>, <tab>, <vertical tab>, that is anything that produces a white space on a printer.
More examples for searches in /usr/dict/words ^qu[a-z]*y$ ^now H[a-z]* H[^A-Z] a[a-z]*e[a-z]*i[a-z]*o[a-z]*u a[^e]*e[^i]*i[^o]*o[^u]*u
Special problem ab. *c what is matched in is it or is it xyabcbbcabbcrt xyabcbbcabbcrt The answer is the longest string that matches! To find smallest substring use ab[^c]*c
Searching for Strings grep global regular expression print – (print: Unix jargon for display) g/re/p see search commands for vi. fgrep egrep - fast grep - extended regular expression
Syntax for egrep + ? * ^ $ ( ) | one ore more occurrences zero or one occurrence zero or more occurrences matches at beginning matches at end escape character grouping or
Examples for egrep "ab+c" matches abc, abbc, … "ab? c" matches only ac and abc "ab*c" matches ac, abbc , … "(a|b)(c|d)" matches ac, ad, bc, bd Other delimiters used by grep, fgrep or egrep: Single quotes, double quotes, or blanks See also related command: look
Character translation tr [-csd] str 1 [str 2] • Options – d delete characters in str 1 instead replacing them with characters in str 2 – s squeeze repetitions of same character into one – c use complement of str 1 (all characters not listed in str 1) • tr uses stdin and stdout
Special characters allowed by tr b <backspace> f <formfeed> n <newline> r <return> t <tab> v <vertical tab> 12 any character given in octal : upper: , : lower:
Examples for tr tr tr "A-Z" "a-z" < inputfile > outputfile "[: upper: ]" "[: lower: ]" –s 'a-z' 'A-Z' '