Unix Lecture 2 Special Characters for Searches - PowerPoint PPT Presentation

About This Presentation
Title:

Unix Lecture 2 Special Characters for Searches

Description:

Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip – PowerPoint PPT presentation

Number of Views:497
Avg rating:3.0/5.0
Slides: 34
Provided by: Hana165
Learn more at: http://plaza.ufl.edu
Category:

less

Transcript and Presenter's Notes

Title: Unix Lecture 2 Special Characters for Searches


1
Unix Lecture 2Special Characters for Searches
SubstitutionsShell Scripts
  • Hana Filip

2
Create a Large Practice File
  • 1, copy RETURN
  • In the visual editor Command Mode
  • Copy all the lines of the file

3
Slash-Search Command
  • /he RETURN
  • Matches all he strings of characters
  • shepherd
  • the
  • their
  • mother

4
Beginnings and Endings of Words
  • /\lthe\gt RETURN
  • \lt beginning of a word
  • \gt ending of a word

5
Beginning of a Line
  • / RETURN
  • Press the n several times and see what happens
  • /The RETURN

6
End of Line Character
  • /werewolf RETURN

7
Finding Blank Lines
  • / RETURN

8
Upper and Lower Case Searches
  • /Fformatting RETURN

9
Locating a Range of Characters
  • /0-9 RETURN
  • /A-Z RETURN
  • /a-z RETURN

10
Excluding Characters from a Search
  • /0-9 RETURN
  • /A-Z RETURN
  • /a-z RETURN

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)

12
Any SINGLE Character
  • /. RETURN
  • /\lt.he\gt RETURN
  • Finds the next example of a three-letter word
    ending in he

13
Repeated 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

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

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

16
Exercise
  • 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

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)

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

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

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

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

22
Exercise
  • Substitute all the occurrences of the word and
    with or
  • g/\ltand\gt/s//or/g RETURN
  • g/spaceandspace/s//spaceorspace/g RETURN

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

24
Global Searches and Substitutions
  • Print a Display of Identified Lines
  • g/Formatted/p RETURN

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

26
Global Searches and Substitutions
  • Removing Blank Lines
  • g//d RETURN

27
Global 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

28
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
Global 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.

30
Global Searches and Substitutions
  • Editor Scripts
  • ex clinton lt script1 RETURN
  • ex execute
  • Output
  • Wills Jefferson Clinton (born Wills Jefferson
  • President of the United States,

31
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
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
  • \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

33
SummaryRegular 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
Write a Comment
User Comments (0)
About PowerShow.com