Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7756017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:42:40+00:00 2026-06-01T12:42:40+00:00

I have (from the sed website http://sed.sourceforge.net/sed1line.txt ) this one-liner: sed -e ‘/./{H;$!d;}’ -e

  • 0

I have (from the sed website http://sed.sourceforge.net/sed1line.txt) this one-liner:

sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d'

Its purpose is to search a paragraph for either AAA, BBB or CCC.

My understanding of the script:

  • ‘/./’ matches every line wich is not empty
  • ‘{}’ all commands within the brackets handle the matched lines
  • ‘H’ appends the holdspace with the matched lines
  • ‘$!d’ delete from patternspace everything but the last line
  • ‘x’ swaps the pattern- and holdspace
  • ‘/AAA/!d’ search for AAA paragraph and print it

What is not clear to me:

  1. In the holdspace should be several separate lines (for each paragraph), why am I able to search the whole paragraph? Are the lines in the holdspace merged to one line?
  2. And how does sed know when one paragraph ends and the other begins in the holdspace?
  3. Why do I have to append ‘$!d’, why is not ‘$d’ sufficient? Why am I not able to omit the ‘-n’ and use ‘$p’ instead of ‘$!d’ in this case?

Thank you very much for every comment!

My test data (match every paragraph with XX in it):

YYaaaa
aaa1
aaa2
aXX3
aaa4

YYbbbb
bbb1
bbb2

YYcccc
ccc1
ccc2
ccc3
cXX4
ccc5

YYdddd
ddd1
dXX2

Following command is used:

sed -ne '/./{H;$!d};x;/XX/p' test2

Versions:

$ sed --version
GNU sed-Version 4.2.1
$ bash --version
GNU bash, Version 4.2.10(1)-release (x86_64-pc-linux-gnu)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:42:41+00:00Added an answer on June 1, 2026 at 12:42 pm

    It collects a paragraph as individual lines into the hold space (H), then when you hit an empty line, /./ fails and it falls through to the x which basically zaps the hold space for the next paragraph.

    In order to correctly handle the final paragraph, it needs to cope with a paragraph which is not followed by an empty line, therefore it falls through from the last line as if it were followed by an empty line. This is a common idiom for scripts which collect something up through a particular pattern (or, to put it differently, it’s a common error for such scripts to fail to handle the last collected data at end of file).

    So in other words, if we are looking at a non-empty line, add it to the hold space, and unless it’s the last line in the file, delete it and start over from the beginning of the script with the next input line. (Perhaps your understanding of d was not complete? This is what $!d means.)

    Otherwise, we have an empty line, or end of file, and the hold space contains zero or more lines of text (one paragraph, possibly empty). Exchange them into the pattern space (the current, empty, line conveniently moves to the hold space) and examine the pattern space. If it fails to match one of our expressions, delete it. Otherwise, the default action is to print the entire pattern space.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read from this article http://codahale.com/how-to-safely-store-a-password/ and it says using a salt isn't
I have used the scroll bar which is extracted from this site http://jscrollpane.kelvinluck.com/arrow_hover.html .
OCR texts often have words that flow from one line to another with a
I have to delete every word containing at least one number from each file
I do have permission to do this. I've got a website with about 250
I have a log file from a web server which looks like this; 1908
I have a website directory where I need to change all hardcoded links from
I have a bunch of files that I am moving from one wiki (Markdown
Here's the code I have: from cStringIO import StringIO from lxml import etree xml
I have derived from a 3rd party class, and when I attempt to call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.