![Page 1: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/1.jpg)
January 23, 2007 Spring 2007 1
Unix Lecture 2Special Characters for Searches & Substitutions
Shell Scripts
Hana Filip
![Page 2: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/2.jpg)
January 23, 2007 Spring 2007 2
Create a Large Practice File
:1,$ copy $ [RETURN]• In the visual editor Command Mode• Copy all the lines of the file
![Page 3: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/3.jpg)
January 23, 2007 Spring 2007 3
Slash-Search Command
/he [RETURN]– Matches all he strings of characters – shepherd – the – their– mother
![Page 4: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/4.jpg)
January 23, 2007 Spring 2007 4
Beginnings and Endings of Words
/\<he\> [RETURN]
\< beginning of a word
\> ending of a word
![Page 5: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/5.jpg)
January 23, 2007 Spring 2007 5
Beginning of a Line
/^ [RETURN]
Press the n several times and see what happens
/^The [RETURN]
![Page 6: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/6.jpg)
January 23, 2007 Spring 2007 6
End of Line Character
$
/werewolf$ [RETURN]
![Page 7: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/7.jpg)
January 23, 2007 Spring 2007 7
Finding Blank Lines
/^$ [RETURN]
![Page 8: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/8.jpg)
January 23, 2007 Spring 2007 8
Upper and Lower Case Searches
/[Ff]ormatting [RETURN]
![Page 9: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/9.jpg)
January 23, 2007 Spring 2007 9
Locating a Range of Characters
/[0-9] [RETURN]
/[A-Z] [RETURN]
/[a-z] [RETURN]
![Page 10: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/10.jpg)
January 23, 2007 Spring 2007 10
Excluding Characters from a Search
/[^0-9] [RETURN]
/[^A-Z] [RETURN]
/[^a-z] [RETURN]
![Page 11: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/11.jpg)
January 23, 2007 Spring 2007 11
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)
![Page 12: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/12.jpg)
January 23, 2007 Spring 2007 12
Any SINGLE Character
/. [RETURN]
/\<.he\> [RETURN]
Finds the next example of a three-letter word ending in he
![Page 13: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/13.jpg)
January 23, 2007 Spring 2007 13
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*
![Page 14: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/14.jpg)
January 23, 2007 Spring 2007 14
Any Number of Any Character
/s.*s [RETURN]
Locates the next line containing an s followed by any sequence of characters followed by s
![Page 15: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/15.jpg)
January 23, 2007 Spring 2007 15
Backslash the Kryptonite of Special
Characters
/\. [RETURN]
Locates the next occurrence of a dot
/^\. [RETURN]
Locates the next line that starts with a dot
![Page 16: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/16.jpg)
January 23, 2007 Spring 2007 16
Exercise
What command would locate the next instance of a sequence of characters starting with psy and ending with y or t?
/\<psy.*[yt]\> [RETURN]
![Page 17: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/17.jpg)
January 23, 2007 Spring 2007 17
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)
![Page 18: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/18.jpg)
January 23, 2007 Spring 2007 18
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”
![Page 19: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/19.jpg)
January 23, 2007 Spring 2007 19
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”
![Page 20: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/20.jpg)
January 23, 2007 Spring 2007 20
Global Searches and Substitutions
• Substitution on All Lines
:g/the/s//mutate [RETURN]g “global”, “all lines”Search for and substitute the first occurrence of the specified word in every line
![Page 21: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/21.jpg)
January 23, 2007 Spring 2007 21
Global Searches and Substitutions
• Substitution for All Occurrences
:g/the/s//mutate/g [RETURN]g “global”, “all lines”Search for and substitute all the occurrences of the specified word in every line
![Page 22: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/22.jpg)
January 23, 2007 Spring 2007 22
Exercise
Substitute all the occurrences of the word and with or
:g/\<and\>/s//or/g [RETURN]
:g/spaceandspace/s//spaceorspace/g [RETURN]
![Page 23: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/23.jpg)
January 23, 2007 Spring 2007 23
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
![Page 24: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/24.jpg)
January 23, 2007 Spring 2007 24
Global Searches and Substitutions
Print a Display of Identified Lines
:g/Formatted/p [RETURN]
![Page 25: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/25.jpg)
January 23, 2007 Spring 2007 25
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]
![Page 26: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/26.jpg)
January 23, 2007 Spring 2007 26
Global Searches and Substitutions
• Removing Blank Lines
:g/^$/d [RETURN]
![Page 27: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/27.jpg)
January 23, 2007 Spring 2007 27
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
![Page 28: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/28.jpg)
January 23, 2007 Spring 2007 28
Global Searches and Substitutions
Editor Scripts% vi script1 [RETURN]g/[0-9]/dg/William/s//Bill/gwq
Your file should contain the above three lineswq is the command to write changes made to the file into the memory of UNIX and return to the Shell
![Page 29: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/29.jpg)
January 23, 2007 Spring 2007 29
Global Searches and Substitutions
Editor ScriptsCreate 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.
![Page 30: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/30.jpg)
January 23, 2007 Spring 2007 30
Global Searches and Substitutions
Editor Scripts
% ex clinton < script1 [RETURN]
ex execute
Output:Wills Jefferson Clinton (born Wills JeffersonPresident of the United States,
![Page 31: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/31.jpg)
January 23, 2007 Spring 2007 31
Global Searches and Substitutions
Editor ScriptsA clean-Up Script% vi script2 [RETURN]g/^$/dg/^ */s/// 2 spacesg/ *$/s/// 2 spacesg/ */s// /g 3 spaces 1 spacewq
![Page 32: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/32.jpg)
January 23, 2007 Spring 2007 32
Summary Meta-Characters
Command Function^ 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
![Page 33: January 23, 2007Spring 20071 Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip](https://reader030.vdocuments.mx/reader030/viewer/2022032414/56649ee85503460f94bf9016/html5/thumbnails/33.jpg)
January 23, 2007 Spring 2007 33
SummaryRegular Characters
Command Functiong 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