Reverse line order in Notepad++ [duplicate]

I want to flip line orders of a document with 500+ lines. The lines aren't just numbers, some include text and other characters. It's a mix.

Example:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6

Which I then want to flip, reverse and look like this from bottom to top:

Line 6
Line 5
Line 4
Line 3
Line 2
Line 1

Solution 1:

Solution not requiring other software except normally-included TextFX plugin:

  1. Edit > Select All
  2. TextFX > TextFX Tools > Insert Line Numbers
  3. If TextFX > TextFX Tools > +Sort ascending is checked, uncheck it
  4. TextFX > TextFX Tools > Sort lines case sensitive (at column)
  5. TextFX > TextFX Tools > Delete Line Numbers or First Word

Solution 2:

This can also be done in Notepad++ without the TextFX plugin. It follows the same strategy of that of the accepted answer, but using native functionality. It is done as follows:

  1. Edit > Select All
  2. Edit > Column Editor... > Select Number to Insert > Set Initial number to 1 > Set Increase by to 1 > Check Leading zeros > Click OK

enter image description here

  1. Edit > Line Operations > Sort Lines in Descending Order Edit: A recent update added extra sorting options, the option: Sort Lines Lexicographically Descending seems to do the job.

enter image description here

  1. Remove Line Numbers through either box selection (Alt+Left Click Drag or Alt+Shift Select) or Search/Replace

enter image description here

Solution 3:

Well, since we are giving code examples, If you are on Windows 7 or you have installed PowerShell on another version of Windows, then:

$foo = New-Object System.collections.arraylist;
$foo.AddRange($(Get-Content 'C:\Path\To\File.txt));
$foo.Reverse();
$foo | Out-File C:\Path\To\File.txt

Or for a non-coding answer, download gVim, open the file and type:

:g/^/m0

Solution 4:

If you're comfortable compiling C++, this should do the trick. Basically, I put each line of the file in a vector, and output it to a new file by using a reverse iterator.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> fileLines;
    std::string              currLine;
    std::ifstream            inFile("input.txt");
    if (inFile.is_open())
    {
        while (inFile.good())
        {
            std::getline(inFile, currLine);
            fileLines.push_back(currLine);
        }
        inFile.close();
    }
    else
    {
        std::cout << "Error - could not open input file!\n";
        return 1;
    }

    std::ofstream outFile("output.txt");
    if (outFile.is_open())
    {
        std::vector<std::string>::reverse_iterator rIt;
        for (rIt = fileLines.rbegin(); rIt < fileLines.rend(); rIt++)
        {
            outFile << *rIt;
        }
        outFile.close();
    }
    else
    {
        std::cout << "Error - could not open output file!\n";
        return 1;
    }
    return 0;
}

If the output file is missing line breaks between the lines, then change the outFile << *rIt; to be outFile << *rIt << "\r\n"; so a line break is added (omit the \r if you're on Unix/Linux).

Disclaimer: I have not tested this code (I wrote it real quick in Notepad), but it looks viable.

Solution 5:

Here is C# .NET code for it I just wrote :)

class Program
{
    static void Main(string[] args)
    {
        try
        {
            String line;
            Stack<String> lines = new Stack<string>();
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader("test.txt"))
            {
                // Read and display lines from the file until the end of
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                    lines.Push(line);
            }

            // Create a writer and open the file
            TextWriter tw = new StreamWriter("test2.txt");
            // Write a line of text to the file
            while (lines.Count > 0)
                tw.WriteLine(lines.Pop());
            // close the stream
            tw.Close();
        }
        catch (Exception e)
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read/written:");
            Console.WriteLine(e.Message);
        }
    }
}