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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:12:45+00:00 2026-05-24T02:12:45+00:00

I have been doing this by hand and I just can’t do it anymore–

  • 0

I have been doing this by hand and I just can’t do it anymore– I have thousands of lines and I think this is a job for sed or awk.

Essentially, we have a file like this:

A sentence X
A matching sentence Y
A sentence Z
A matching sentence N

This pattern continues for the entire file. I want to flip every sentence and matching sentence so the entire file will end up like:

A matching sentence Y
A sentence X
A matching sentence N
A sentence Z

Any tips?

edit: extending the initial problem

Dimitre Radoulov provided a great answer for the initial problem. This is an extension of the main problem– some more details:

Let’s say we have an organized file (due to the sed line Dimitre gave, the file is organized). However, now I want to organize the file alphabetically but only using the language (English) of the second line.

watashi 
me
annyonghaseyo
hello
dobroye utro!
Good morning!

I would like to organize alphabetically via the English sentences (every 2nd sentence). Given the above input, this should be the output:

dobroye utro!
Good morning!
annyonghaseyo
hello
watashi
me 
  • 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-24T02:12:46+00:00Added an answer on May 24, 2026 at 2:12 am
    sed 'N; 
    s/\(.*\)\n\(.*\)/\2\
    \1/' infile
    

    N – append the next line of input into the pattern space
    \(.*\)\n\(.*\) – save the matching parts of the pattern space
    the one before and the one after the newline.
    \2\\
    \1
    – exchange the two lines (\1 is the first saved part,
    \2 the second). Use escaped literal newline for portability

    With some sed implementations you could use the escape sequence
    \n: \2\n\1 instead.

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

Sidebar

Related Questions

I have been doing the booking form on this website here. http://offline.raileisure.com/ if you
I have been given a project to edit. I think this is a simple
We have been doing some research into physically isolating the secure and non-secure sections
I have been doing a little reading on Flow Based Programming over the last
I have been doing some research on test driven development and find it pretty
I have been doing active development in C# for several years now. I primarily
I have been doing TDD and was using it more as unit testing than
I have been doing PHP stuff for almost one year and I have never
I've been doing some Web-Projects lately that rely on heavy Data-Binding and have been
I use Netbeans for development and have been doing my Mercurial operations on the

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.