Presentation is loading. Please wait.

Presentation is loading. Please wait.

January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip.

Similar presentations


Presentation on theme: "January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip."— Presentation transcript:

1 January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip

2 January 23, 2007Spring 20072 Create a Large Practice File :1,$ copy $ [RETURN] In the visual editor Command Mode Copy all the lines of the file

3 January 23, 2007Spring 20073 Slash-Search Command /he [RETURN] –Matches all he strings of characters –s he p he rd –t he –t he ir –mot he r

4 January 23, 2007Spring 20074 Beginnings and Endings of Words /\ [RETURN] \< beginning of a word \> ending of a word

5 January 23, 2007Spring 20075 Beginning of a Line /^ [RETURN] Press the n several times and see what happens /^The [RETURN]

6 January 23, 2007Spring 20076 End of Line Character $ /werewolf$ [RETURN]

7 January 23, 2007Spring 20077 Finding Blank Lines /^$ [RETURN]

8 January 23, 2007Spring 20078 Upper and Lower Case Searches /[Ff]ormatting [RETURN]

9 January 23, 2007Spring 20079 Locating a Range of Characters /[0-9] [RETURN] /[A-Z] [RETURN] /[a-z] [RETURN]

10 January 23, 2007Spring 200710 Excluding Characters from a Search /[^0-9] [RETURN] /[^A-Z] [RETURN] /[^a-z] [RETURN]

11 January 23, 2007Spring 200711 Several Identifiers /[^0-9A-Za-z] [RETURN] Will go to those characters not numbers, nor upper or lower case letters Scope of ^ ‘negation’: ^(0-9A-Za-z)

12 January 23, 2007Spring 200712 Any SINGLE Character /.[RETURN] /\ [RETURN] Finds the next example of a three-letter word ending in he

13 January 23, 2007Spring 200713 Repeated Characters /ol* [RETURN] –Finds all words that have an o followed by zero or more l ’s –* Kleene star, means “any possible number (including zero) of the previous character” –Locate sets of two or more identical characters x: xxx*

14 January 23, 2007Spring 200714 Any Number of Any Character /s.*s [RETURN] Locates the next line containing an s followed by any sequence of characters followed by s

15 January 23, 2007Spring 200715 Backslash the Kryptonite of Special Characters /\. [RETURN] Locates the next occurrence of a dot /^\. [RETURN] Locates the next line that starts with a dot

16 January 23, 2007Spring 200716 Exercise What command would locate the next instance of a sequence of characters starting with psy and ending with y or t? /\ [RETURN]

17 January 23, 2007Spring 200717 Global Searches and Substitutions Substitutions within the current line :/the [RETURN] Locates the next line containing the word the (the same as /the [RETURN] ) and makes that line the current line : s/the/some [RETURN] Locates the next occurrence of the word the on the current line and substitutes it with some,otherwise fails (if the is NOT on the current line)

18 January 23, 2007Spring 200718 Global Searches and Substitutions Substitution for the Next Occurrence :/the/s/the/some [RETURN] :/the “find the next line containing the word the and make that line the current line” /s/the/some “substitute for the word the the word some ”

19 January 23, 2007Spring 200719 Global Searches and Substitutions The Easier Way to Substitute for the Next Occurrence :/the/s/the/some [RETURN] :/the/s//some [RETURN] // “the last string of characters mentioned”

20 January 23, 2007Spring 200720 Global Searches and Substitutions Substitution on All Lines :g/the/s//mutate [RETURN] g “ g lobal”, “all lines” Search for and substitute the first occurrence of the specified word in every line

21 January 23, 2007Spring 200721 Global Searches and Substitutions Substitution for All Occurrences :g/the/s//mutate/g [RETURN] g “ g lobal”, “all lines” Search for and substitute all the occurrences of the specified word in every line

22 January 23, 2007Spring 200722 Exercise Substitute all the occurrences of the word and with or :g/\ /s//or/g [RETURN] :g/ space and space /s// space or space /g [RETURN]

23 January 23, 2007Spring 200723 Summary of Substitution Commands :/word/s//newword [RETURN] Changes the first occurrence in first line :g/word/s//newword [RETURN] Changes the first occurrence in all lines :g/word/s//newword/g [RETURN] Changes all occurrences in all lines

24 January 23, 2007Spring 200724 Global Searches and Substitutions Print a Display of Identified Lines :g/Formatted/p [RETURN]

25 January 23, 2007Spring 200725 Global Searches and Substitutions Removing Multiple Spaces :g/ 3spaces */s// 1space /g [RETURN] Substitutes 2 or more spaces with 1 space :g/^ 3spaces */s// 1space /g [RETURN] :g/ 3spaces *$/s// 1space /g [RETURN]

26 January 23, 2007Spring 200726 Global Searches and Substitutions Removing Blank Lines :g/^$/d[RETURN]

27 January 23, 2007Spring 200727 Global Searches and Substitutions Adding to Words :g/cover\>/s//&ing/p [RETURN] & the last pattern found p prints the changed line(s) on the terminal :g/\<terre.*/s//extra&/p [RETURN] extra is prefixed to terrestrial

28 January 23, 2007Spring 200728 Global Searches and Substitutions Editor Scripts % vi script1[RETURN] g/[0-9]/d g/William/s//Bill/g wq Your file should contain the above three lines wq is the command to write changes made to the file into the memory of UNIX and return to the Shell

29 January 23, 2007Spring 200729 Global Searches and Substitutions Editor Scripts Create a file entitled clinton William Jefferson Clinton (born William Jefferson Blythe III[1] on August 19, 1946) was the 42nd President of the United States, serving from 1993 to 2001.

30 January 23, 2007Spring 200730 Global Searches and Substitutions Editor Scripts % ex clinton < script1[RETURN] ex execute Output: Wills Jefferson Clinton (born Wills Jefferson President of the United States,

31 January 23, 2007Spring 200731 Global Searches and Substitutions Editor Scripts A clean-Up Script % vi script2[RETURN] g/^$/d g/^ */s/// 2 spaces g/ *$/s/// 2 spaces g/ */s// /g 3 spaces 1 space wq

32 January 23, 2007Spring 200732 Summary Meta-Characters CommandFunction ^match beginning of line $ match end of line. match any single character * match any number of occurrences of previous character (also 0) [ ]match any character (or range of characters enclosed within the brackets [^ ]match any character (or range of characters NOT enclosed within the brackets \< match beginning of a word or a phrase >\ match ending of a word or a phrase & replace ‘target character(s)’ with last character pattern encountered \ remove ‘magic’ of Special Characters (Kryptonite) / /match last pattern in search

33 January 23, 2007Spring 200733 Summary Regular Characters Command Function g at the beginning of a search: all lines in the file at end of a search: all cases of pattern within specified lines s substitute for last pattern found p at end of a search: display pattern found on your screen d at end of a search: delete all lines in which pattern is found


Download ppt "January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip."

Similar presentations


Ads by Google