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

  • SEARCH
  • Home
  • 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 7611543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:41:20+00:00 2026-05-31T01:41:20+00:00

I need to remove a blank line prior to a match. So given the

  • 0

I need to remove a blank line prior to a match.

So given the file:

random text
more random text






#matchee

I need to match the pattern /#matchee/, then delete the blank line before it.

Here’s what I’ve tried — no success:

sed '/^[ \t]*$/{N;/#matchee.*$/{D;}}' file.txt

My logic:

  • If blank line; append next line to pattern space yielding /blank line\n…etc/
  • If pattern space contains /#matchee/, Delete up to newline yielding /#matchee/

Basically /matchee/ is a constant that must be maintained and I need to remove an extra
blank line from each record in a file of records delimited by /#matchee/.

This produces no effect whatever. I am RTFM-ing and D is supposed to delete pattern
space up to the newline. Since N appends a newline plus next line — this should produce the desired results ….alas ….no and no again. Is this because the match contains essentially nothing ( blankline )?

  • 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-05-31T01:41:21+00:00Added an answer on May 31, 2026 at 1:41 am

    Your approach will work if there are an odd number of blank lines, but it will fail if there are an even number. Your sed script is a loop of the following.

    • grab a line L1, and add it to the pattern space
    • if L1 is non-blank, print it; but if it is blank, then:
      • grab another line, L2, and add it to the pattern space
      • if L2 contains #matchee, discard L1 from the pattern space
      • print the pattern space, which is consists either of L1 and L2, or just of L2

    You’ll notice that L2 is always printed, even if it’s blank and followed by a line that contains #matchee. It’s protected by the fact that it immediately follows an odd number of blank lines.

    Edited to add: To fix the above-described problem, you can add an inner loop by using the : command to create a label and the b command to “branch” to it (goto). This:

    sed '/^[ \t]*$/{: a;N;/#matchee/!P;D;/^[ \t]*$/b a}' file.txt
    

    is a loop of the following:

    • grab a line L1, and add it to the pattern space
    • if L1 is non-blank, print it; but if it is blank, then:
      • create label a ← this is a no-op, just a place to goto
      • grab another line, L2, and add it to the pattern space
      • if L2 does not contain #matchee, print L1
      • discard L1 from the pattern space (whether or not we printed it)
      • we can now think of L2 as L1; it’s the only thing in the pattern space
      • if the rechristened L1 is blank, goto a
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file that I need to reformat and remove extra blank lines.
I have an XML file. There is some blank line in the file. How
I need a one liner using sed, awk or perl to remove blank lines
I need to remove blank lines (with whitespace or absolutely blank) in PHP. I
I need to remove duplicate rows from a fairly large SQL Server table (i.e.
I need to remove temp files on Tomcat startup, the pass to a folder
I need to remove multiple items from a Dictionary. A simple way to do
I need to remove the following format from the end of a string in
I need to remove whitespaces after the word in the string. Can this be
I need to remove all entries in a dictionary accordingly to a specified lower

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.