135

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
random
  • 15,201
Danpe
  • 1,501

7 Answers7

118

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
Gnubie
  • 2,943
71

Edit: Newer versions of Notepad++ (v8.0+) now directly support reversing line order. Simply go to:

Edit -> Line Operations -> Reverse Line Order

Note that it will reverse the order of selected lines, or reverse all lines in the file if nothing is selected.

Since the menu is quite full, here is a visual hint:

position of the command in the elaborate menu structure

For older versions of Notepad++, the previous strategy can still be used.


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

qubodup
  • 9,394
9

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
phuclv
  • 30,396
  • 15
  • 136
  • 260
EBGreen
  • 9,655
5

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(&quot;output.txt&quot;);
if (outFile.is_open())
{
    std::vector&lt;std::string&gt;::reverse_iterator rIt;
    for (rIt = fileLines.rbegin(); rIt &lt; fileLines.rend(); rIt++)
    {
        outFile &lt;&lt; *rIt;
    }
    outFile.close();
}
else
{
    std::cout &lt;&lt; &quot;Error - could not open output file!\n&quot;;
    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.

phuclv
  • 30,396
  • 15
  • 136
  • 260
Breakthrough
  • 34,847
1

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(&quot;test2.txt&quot;);
        // Write a line of text to the file
        while (lines.Count &gt; 0)
            tw.WriteLine(lines.Pop());
        // close the stream
        tw.Close();
    }
    catch (Exception e)
    {
        // Let the user know what went wrong.
        Console.WriteLine(&quot;The file could not be read/written:&quot;);
        Console.WriteLine(e.Message);
    }
}

}

phuclv
  • 30,396
  • 15
  • 136
  • 260
Danpe
  • 1,501
1

Here is a non-coding way:

  1. Download/Install TextPad free trial
  2. Open a spreadsheet program (ie Excel) and create numbers 1000 through 1500 in column "A" by putting a "1000" in cell A1, then putting A1+1 in cell A2, then copy that down to A500.
  3. Open your text file in TextPad
  4. Change to "block mode" in TextPad
  5. Paste column A from the spreadsheet into TextPad (all will end up on the left margin due to block mode)
  6. Use TextPad sort feature, descending
  7. Use TextPad block mode delete to get rid of the numbers
Dale
  • 531
  • 3
  • 17
0

If you wish to automate this on Notepad++ with a single click:

  1. Get this Python script plugin
  2. add the code below and save it as .py file in the Python script folder (to know the location of this folder, click plugins tab, then Python Script and new scripts).

All the glory to Reck Dickhard!

To add it in the context menu:

If you click on the plugins tab → Python ScriptConfiguration, you can assign the script either to a toolbar icon, or to the Python Script menu itself. (If you assign a script to the menu, then it will appear immediately, but you will not be able to assign a shortcut to it until next time Notepad++ starts. If you assign it to a toolbar icon, then it will only appear on the next start of Notepad++.)

JinSnow
  • 912