Upload
juniper-parks
View
218
Download
0
Tags:
Embed Size (px)
Citation preview
January 23, 2007 Spring 2007 1
Unix Lecture 2Special Characters for Searches & Substitutions
Shell Scripts
Hana Filip
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
January 23, 2007 Spring 2007 3
Slash-Search Command
/he [RETURN]– Matches all he strings of characters – shepherd – the – their– mother
January 23, 2007 Spring 2007 4
Beginnings and Endings of Words
/\<he\> [RETURN]
\< beginning of a word
\> ending of a word
January 23, 2007 Spring 2007 5
Beginning of a Line
/^ [RETURN]
Press the n several times and see what happens
/^The [RETURN]
January 23, 2007 Spring 2007 6
End of Line Character
$
/werewolf$ [RETURN]
January 23, 2007 Spring 2007 7
Finding Blank Lines
/^$ [RETURN]
January 23, 2007 Spring 2007 8
Upper and Lower Case Searches
/[Ff]ormatting [RETURN]
January 23, 2007 Spring 2007 9
Locating a Range of Characters
/[0-9] [RETURN]
/[A-Z] [RETURN]
/[a-z] [RETURN]
January 23, 2007 Spring 2007 10
Excluding Characters from a Search
/[^0-9] [RETURN]
/[^A-Z] [RETURN]
/[^a-z] [RETURN]
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)
January 23, 2007 Spring 2007 12
Any SINGLE Character
/. [RETURN]
/\<.he\> [RETURN]
Finds the next example of a three-letter word ending in he
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*
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
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
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]
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)
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”
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”
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
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
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]
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
January 23, 2007 Spring 2007 24
Global Searches and Substitutions
Print a Display of Identified Lines
:g/Formatted/p [RETURN]
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]
January 23, 2007 Spring 2007 26
Global Searches and Substitutions
• Removing Blank Lines
:g/^$/d [RETURN]
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
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
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.
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,
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
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
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