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 7654305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:13:18+00:00 2026-05-31T12:13:18+00:00

I am using the following command to find a pattern spanning multiple lines and

  • 0

I am using the following command to find a pattern spanning multiple lines and replacing it with a blank line:

sed -n '1h; 1!H; ${ g; s/<Pattern1>\n<pattern2> //g p }' <file-name>

For example, to find the pattern
John
Doe

in a file test.txt which contains the following entries:

Mary

John
Doe
Simon
Henry

I would use the command:

sed -n  '1h; 1!H; ${ g; s/John\nDoe //g p }' test.txt

However, I can’t seem to make this command work for a pattern which has the second line as a blank, i.e. using the ^$ character.

Thus, if I were to search for the multi-line pattern, Mary followed by a blank line, I do not see any matching patterns. Thus, the following command causes no searches and thus no substitutions.

sed -n  '1h; 1!H; ${ g; s/Mary\n^$ //g p }' test.txt

Any thoughts?

EDIT


However, if I store multiple such command in a file and execute them, then the first search goes through fine, however on searching for subsequent patterns, I get a blank output from sed.

That is if I store, sed -n ‘1h; 1!H; ${ g; s/\n //g p }’ sed -n ‘1h; 1!H; ${ g; s/\n //g p }’ in a file and then execute each line in the this file using the eval keyword, then the first sed replaces the first multi-line patterns, i.e. pattern1, followed by pattern2, however, it returns a blank output even though the file contains these patterns. Any clues?

  • 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-31T12:13:20+00:00Added an answer on May 31, 2026 at 12:13 pm

    This works for me with your input file:

    sed -n  '1h; 1!H; ${ g; s/Mary\n\n/\n/g p }'
    

    Explanation:

    Mary\n\n                # Mary with two consecutive newlines, the first one is automatically added by
                            # the 'H' command, and the next one means the empty line, asumming it has no
                            # spaces.
    \n                      # Substitute it with a blank line.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following command line output using grep: [gjempty@gjempty]$ find . -name *.php |
I am using the following command on command line for getting the pattern matched
I launch the following command line (process) from a Windows VC++ 6 program using
I am using the following command to grep stuff in subdirs find . |
I'm using the following command to execute a PHP file via cron php -q
I'm new to vim and I find that I am using the following command
I created a j2ee-simple project using the maven template with following command-line mvn archetype:create
I'm using the following regex find command in OS X terminal to find a
I find we can start monkey from workstation, by using the following command: adb
I am currently using the following command to upload my site content: scp -r

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.