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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:06:37+00:00 2026-05-18T09:06:37+00:00

How can I make use of the sed H , h , x ,

  • 0

How can I make use of the sed H, h, x, g, G etc. commands to swap two lines?

For example in the file

START
this is a dog
this is a cat
this is something else
END

say I want to swap “this is a dog” with “this is something else”.

This is what I have so far:

/this is a dog/{
 h # put to hold space 
}
/this is something else/{
 # now i am stuck on what to do.
}
  • 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-18T09:06:38+00:00Added an answer on May 18, 2026 at 9:06 am

    If you know a pattern on each of the two lines you want to swap, but not the full contents of the lines, you can do something like this:

    sed -n '                     # turn off default printing
        /dog/{                   # if the line matches "dog"
               h                 # put it in hold space
               :a                # label "a" - the top of a loop
               n                 # fetch the next line
               /something/{      # if it matches "something"
                           p     # print it
                           x     # swap hold and pattern space
                           bb    # branch out of the loop to label "b"
               }                 # done with "something"
                                 # if we're here, the line doesn't match "something"
               H                 # append pattern space to hold space
               x                 # swap hold and pattern space
               s/\([^\n]*\)\n\([^\n]*\)$/\2\n\1/    # see below
               x                 # swap hold and pattern space
               ba                # branch to the top of the loop to label "a"
        }                        # done with "dog"
        :b                       # label "b" - outside the loop
                                 # print lines that don't match and are outside the pair
        p                        # also prints what had been accumulating in hold space
        ' inputfile
    

    The substitution pattern keeps “dog” at the end of the accumulated lines. It keeps swapping the last two lines that we’re keeping in hold space so that “dog” “bubbles” to the bottom.

    For example, let’s put another line after the “cat” line so the process is a little clearer. We’ll ignore the lines before “dog” and after “something”. And I’ll continue to refer to the lines using my nicknames

    this is a dog
    this is a cat
    there's a bear here, too
    this is something else
    

    “Dog” is read, then “cat” is fetched. Some appending and swapping is done. Now pattern space looks like this (\N represents a newline, I’m using an upper case “N” so it stands out, the ^ is the beginning of the pattern space and $ is the end):

    ^this is a dog\Nthis is a cat$
    

    The substitution command looks for any number of characters that are not newlines (and captures them) followed by a newline followed by any number of characters that are not newlines (and captures them) that are at the end of the line ($) and replaces all that with the two captured strings in the reverse order separated by a newline. Now pattern space looks like this:

    ^this is a cat\Nthis is a dog$
    

    Now we swap and read a new line. It’s not “something” so we do some appending and swapping and now we have:

    ^this is a cat\Nthis is a dog\Nthere's a bear here, too$
    

    We do the substitution again and get:

    ^this is a cat\Nthere's a bear here, too\Nthis is a dog$
    

    Why didn’t we get “bear/dog/cat” instead? Because the regex pattern consisting of two lines (which each, as usual, consist of non-newlines followed by a newline) is anchored at the end of the line using the $ so we’re ignoring anything that comes before it. Note that the last newline is implied and doesn’t actually exist in pattern or hold space. That’s why I’m not showing it here.

    Now we read “something” and print it. We swap. Hey! there’s that stuff that we’ve been “bubbling”. Branch and print. Since “dog” is at the bottom of the lines (that had been accumulated in hold space) and we printed “something” right before that bunch, the effect is that we swapped the two lines.

    This script will work regardless of how many lines appear before, between or after the two lines to be swapped. In fact, if there are multiple pairs of matching lines, the members of each pair will be swapped throughout the file.

    As you can see, I’m keying on just one word in the lines of interest, but any suitable regular expression would do.

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

Sidebar

Related Questions

We have a process which can use a file containing sed commands to alter
How can I make this link use the child selector without changing or removing
I use plugin wp-type, there I can make costum pages, this plugin have function
According to this , you can make use of xs:key and xs:keyref when marshalling
I would like to know which pattern can I use in sed to make
In my application i want to use a functionality which can make the vibrate
Can I make in MediaWiki links lowercase and use hyphen instead of underscore as
How can I make my class library STA for use with CreateObject in VBScript?
Can we make a class copy constructor virtual in C++? How to use?
I'm trying to use ON CASCADE DELETE in mysql db but I can't make

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.