Title: Unix Lecture 2 Special Characters for Searches
1Unix Lecture 2Special Characters for Searches
SubstitutionsShell Scripts
2Create a Large Practice File
- 1, copy RETURN
- In the visual editor Command Mode
- Copy all the lines of the file
3Slash-Search Command
- /he RETURN
- Matches all he strings of characters
- shepherd
- the
- their
- mother
4Beginnings and Endings of Words
- /\lthe\gt RETURN
- \lt beginning of a word
- \gt ending of a word
5Beginning of a Line
- / RETURN
- Press the n several times and see what happens
- /The RETURN
6End of Line Character
7Finding Blank Lines
8Upper and Lower Case Searches
9Locating a Range of Characters
- /0-9 RETURN
- /A-Z RETURN
- /a-z RETURN
10Excluding Characters from a Search
- /0-9 RETURN
- /A-Z RETURN
- /a-z RETURN
11Several 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)
12Any SINGLE Character
- /. RETURN
- /\lt.he\gt RETURN
- Finds the next example of a three-letter word
ending in he
13Repeated Characters
- /ol RETURN
- Finds all words that have an o followed by zero
or more ls - Kleene star, means any possible number
(including zero) of the previous character - Locate sets of two or more identical characters
x - xxx
14Any Number of Any Character
- /s.s RETURN
- Locates the next line containing an s followed
by any sequence of characters followed by s
15Backslash the Kryptonite of Special Characters
- /\. RETURN
- Locates the next occurrence of a dot
- /\. RETURN
- Locates the next line that starts with a dot
16Exercise
- What command would locate the next instance of a
sequence of characters starting with psy and
ending with y or t? - /\ltpsy.yt\gt RETURN
17Global 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)
18Global 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
19Global 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
-
20Global 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
21Global 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
22Exercise
- Substitute all the occurrences of the word and
with or - g/\ltand\gt/s//or/g RETURN
- g/spaceandspace/s//spaceorspace/g RETURN
23Summary 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
24Global Searches and Substitutions
- Print a Display of Identified Lines
- g/Formatted/p RETURN
25Global 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
26Global Searches and Substitutions
- Removing Blank Lines
- g//d RETURN
27Global Searches and Substitutions
- Adding to Words
- g/cover\gt/s//ing/p RETURN
- the last pattern found
- p prints the changed line(s) on the terminal
- g/\ltterre./s//extra/p RETURN
- extra is prefixed to terrestrial
28Global 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
29Global Searches and Substitutions
- Editor Scripts
- Create a file entitled clinton
- William Jefferson Clinton (born William Jefferson
- Blythe III1 on August 19, 1946)
- was the 42nd
- President of the United States,
- serving from 1993 to 2001.
30Global Searches and Substitutions
- Editor Scripts
- ex clinton lt script1 RETURN
- ex execute
- Output
- Wills Jefferson Clinton (born Wills Jefferson
- President of the United States,
31Global 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
32Summary 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
- \lt match beginning of a
word or a phrase - gt\ 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
33SummaryRegular 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