Advanced Topics in sed - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Advanced Topics in sed

Description:

n - Copy the pattern space to standard output. ... N - Append the next line of input to the pattern space with an embedded NEWLINE. ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 14
Provided by: Kenneth4
Category:
Tags: advanced | pattern | sed | topics

less

Transcript and Presenter's Notes

Title: Advanced Topics in sed


1
Advanced Topics in sed
2
How Does sed Treat Files?
Input
scriptfile
Input line (Pattern Space)
Hold Space
Output
3
Working With Multi-line Pattern Space
  • Up until now, we have only considered operating
    on single lines
  • Although this is easy, it makes looking for
    phrases or expressions that cross line boundaries
    difficult
  • sed has the ability to look at more than one line
    at a time, which allows you to extend your
    pattern search space over multiple lines
  • The commands n, d, and p all have multi-line
    versions

4
Relevant Commands
  • n - Copy the pattern space to standard output.
    Replace the pattern space with the next line of
    input
  • N - Append the next line of input to the pattern
    space with an embedded NEWLINE. (The current
    line number changes.)
  • d - Delete the pattern space. Start the next
    cycle.
  • D - Delete the initial segment of the pattern
    space through the first NEWLINE. Start the next
    cycle.
  • p - Print. Copy the pattern space to standard
    output.
  • P - Copy the initial segment of the pattern space
    up through the first NEWLINE to standard output.

5
  • For example, suppose I want to find all
    occurrences of "the BSD Unix system" and replace
    it with "the Linux system"
  • I could start with
  • s/BSD Unix/Linux/
  • but this would substitute Linux EVERYWHERE "BSD
    Unix" occurred
  • So, let's use
  • s/the BSD Unix system/the Linux system/
  • This is fine but what if my input text looks like
    this
  • .the BSD
  • Unix system ...

6
Bummer! No Match!
  • So, how about if I use the multi-line commands?
  • /the/
  • N
  • s/the \nBSD \nUnix \nsystem/the Linux
    system/
  • This will work no matter where the newline is but
    it seems awfully hard, and it is
  • What if I replace the newline BEFORE I do the
    pattern match?

7
  • s/the BSD Unix system/the Linux system/
  • /the/
  • N
  • s/ \n/ /
  • s/the BSD Unix system /the Linux system\
  • /
  • This first looks for a line where the search
    pattern phrase is contained on the same line
  • If not, then we look for the case where it is
    split across lines by appending lines, replacing
    the embedded newline and then doing the
    substitution, followed by an escaped newline

8
The hold space
  • The pattern space is a buffer that holds the
    current input line
  • There is another buffer that is under programmer
    control called the hold space
  • The contents of pattern space can be copied to
    and from hold space
  • The hold space is used for temporary storage
  • Commands cannot access it except for the ones
    that copy between it and pattern space

9
Copy Pattern Space to Hold Space
  • The h and H commands move the contents of pattern
    space to hold space
  • h copies pattern space to hold space, replacing
    anything that was previously there
  • H appends an embedded NEWLINE ("\n") to whatever
    is currently in hold space followed by the
    contents of pattern space
  • Even if the hold space is empty, the embedded
    NEWLINE is appended to hold space first

10
Get Contents of Hold Space
  • g and G get the contents of hold space and place
    it in pattern space
  • g copies the contents of hold space into pattern
    space, replacing whatever was there
  • G appends an embedded NEWLINE character ("\n")
    followed by the contents of hold space to pattern
    space
  • Even if pattern space is empty, the NEWLINE is
    still appended to pattern space before the
    contents of the hold space

11
  • Now, suppose that I want to capitalize a specific
    word in a file, specifically, every time I see a
    "the abc statement" I want to change it to the
    ABC statement
  • A script to do this looks like this
  • /the . statement/
  • h
  • s/.the \(.\) statement./\1/
  • y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTU
    VWXYZ/
  • G
  • s/\(.\)\n(.the \).\( statement.\)/\2\1\3/

12
So How Does It Work?
  • The address limits the procedure to lines that
    match "the . statement"
  • h copies the current line into hold space,
    replacing whatever was there
  • After the h, pattern space and hold space are
    identical
  • pattern space "find the print statement"
  • hold space "find the print statement"
  • s/.the \(.\) statement. /\1/ extracts the name
    of the statement (\1) and replaces the entire
    line with it
  • pattern space "print"
  • hold space "find the print statement"

13
  • y/abc./ABC/ changes each lowercase letter to
    uppercase
  • pattern space "PRINT"
  • hold space - "find the print statement"
  • The G command appends a NEWLINE ("\n") to pattern
    space followed by the line saved in hold space
  • s/\(.\)\n(.the \).\( statement.\)/\2\1\3/
    matches three different parts of the pattern
    space and rearranges them
Write a Comment
User Comments (0)
About PowerShow.com