Results 1 to 8 of 8

Thread: Help wanted to understand the "g" command in sed

Threaded View

  1. #1
    Join Date
    Dec 2007
    Beans
    9,273
    Distro
    Lubuntu 16.04 Xenial Xerus

    Help wanted to understand the "g" command in sed

    I made these two small files:
    a1.txt
    this is the first line
    this is the second line
    there are two lines above this one
    and
    a2.txt
    this is line a
    this is line b
    lines a and b are above this one

    Then, to recover just the lines from each file containing "above", I can use:
    Code:
    sed -nrs '/above/p' a*.txt
    To introduce blank lines below each of the lines, I can use:
    Code:
    sed -nrs '/above/p;${g;p}' a*.txt
    I want help to understand the function of g in the second code.
    I looked at info sed and it has this to say about g:
    "Replace the contents of the pattern space with the contents of the hold space."

    My understanding is that each line is stored in the "pattern space" and then processed by the specified commands, but at what point does the "hold space" figure and how does that end up providing a blank line?

    Basically, what does "${g;p}" do? I know it works. But how?
    Last edited by vasa1; March 18th, 2013 at 04:52 PM. Reason: marked as [SOLVED]
    Shortened urls: See point #11 here. And clicking on them could be risky.
    Use ubuntu-support-status with caution.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •