Quotes: single vs. double vs. grave accent % set day = date % echo day 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 commandsvi 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/SVR4/g • Searches for a line containing Unix and in this line replaces all occurrences of System with SVR4 • :g/Unix/s/System/SVR4/g • Searches for all lines containing Unix and in these lines replaces all occurrences of System with SVR4 • :g/Unix/s/System/SVR4/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 •  searches for the number 9 • \[9\] searches for  • \.\.\. Searches for three dots … • \\\\ Searches for \\ • [A-Z] Searches for any capital letter • ^Unix Searches for Unix at start of line • Unix$ Searches for Unix at end of line • [^A-Z] Searches for characters other than capital letters • A.C matches AxC with x any character • A.*C 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][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 xyabcbbcabbcrt is it xyabcbbcabbcrt oris it xyabcbbcabbcrt oris it xyabcbbcabbcrt oris it 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 - fast grep egrep - 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, abc, 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] str1 [str2] • Options • d delete characters in str1 instead replacing them with characters in str2 • s squeeze repetitions of same character into one • c use complement of str1 (all characters not listed in str1) • truses stdin and stdout
Special characters allowed by tr \b <backspace> \f <formfeed> \n <newline> \r <return> \t <tab> \v <vertical tab> \012 any character given in octal :upper:, :lower:
Examples for tr tr "A-Z" "a-z" < inputfile > outputfile tr "[:upper:]" "[:lower:]" tr –s 'a-z' 'A-Z' tr '\012' ':' replace all linefeeds to :
Expansion of tabs • tr can translate a tab into a single character, • To expand it into several blank spaces use expand or unexpand • For more general translations need to use sed stream editor.
Stream editor • Read next input line from stdin into buffer. • Apply all given editing commands to buffer • Write buffer to stdout • Go to step 1 Editing commands subset of ex (or vi). sed can do most things that grep can do.
Noninteractive editing sed 's/Unix/UNIX/g' < chapter1 Warning: Do not give command sed 's/Unix/UNIX/g' <chapter1 >!chapter1 It will delete the file. Input file can also be given as a parameter: sed 's/Unix/UNIX/g;s/ucb/UCB/g' chapter1
Noninteractive editing continued sed '/^$/d' file removes empty lines sed '/^[ ]*$/d' file removes blank and empty lines sed 's/$/ /' file might be the first attempt to doublespace. First mistake: After sed 's/$/ need to type <cr> and UNIX will take that it is time to execute command. To continue command on next line need to quote <cr> i.e. sed 's/$/\ /' file
Noninteractive editing corrected Second mistake, <cr> now absorbed by quote. Correct version uses script file (option –f) sed -f double file Where script file double holds editing command s/$/\ /
Format of each man page Name name and purpose of command Syntax Description may be long Options Files list of files important for this command Return values Diagnostics possible error and warning messages Bugs known errors or short comings See also related information