Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 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

3 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/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

4 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

5 Examples of search patterns [9] searches for the number 9 \[9\] searches for [9] \.\.\. 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

6 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 as a complete word, surrounded by blanks or tabs Whitespaces include,,,,,, that is anything that produces a white space on a printer.

7 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

8 Special problem ab.*c what is matched in xyabcbbcabbcrt is it xyabcbbcabbcrt or is it xyabcbbcabbcrt The answer is the longest string that matches! To find smallest substring use ab[^c]*c

9 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

10 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

11 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

12 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) tr uses stdin and stdout

13 Special characters allowed by tr \b \f \n \r \t \v \012 any character given in octal :upper:, :lower:

14 Examples for tr tr "A-Z" "a-z" outputfile tr "[:upper:]" "[:lower:]" tr –s 'a-z' 'A-Z' tr '\012' ':' replace all linefeeds to :

15 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.

16 Stream editor 1.Read next input line from stdin into buffer. 2.Apply all given editing commands to buffer 3.Write buffer to stdout 4.Go to step 1 Editing commands subset of ex (or vi). sed can do most things that grep can do.

17 Noninteractive editing sed 's/Unix/UNIX/g' < chapter1 Warning: Do not give command sed 's/Unix/UNIX/g' !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

18 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 and UNIX will take that it is time to execute command. To continue command on next line need to quote i.e. sed 's/$/\ /' file

19 Noninteractive editing corrected Second mistake, now absorbed by quote. Correct version uses script file (option –f) sed -f double file Where script file double holds editing command s/$/\ /

20 Format of each man page Namename and purpose of command Syntax Descriptionmay be long Options Fileslist of files important for this command Return values Diagnosticspossible error and warning messages Bugsknown errors or short comings See alsorelated information


Download ppt "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."

Similar presentations


Ads by Google