Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
share|improve this question
    
Meaning you want the numbers on the left margin to count down, or you want the actual content of your file to be reversed? –  Maxpm Sep 2 '11 at 12:30
1  
@Maxpm Actual content of the file to be reversed. –  Danpe Sep 2 '11 at 12:40
    
possible duplicate of Reverse line order in Notepad++ –  Franck Dernoncourt Feb 28 at 15:49
add comment

6 Answers

up vote 18 down vote accepted

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
share|improve this answer
2  
If TextFX is not found then go to: Plugins » Plugin Manager » Show Plugin Manager » Check "TextFX Characters", Click Install. See here: textfx.no-ip.com/textfx –  Kalpesh Jul 25 '12 at 16:18
    
-what worked was "Sort lines case insensitive" in part 4) –  Theta30 Apr 1 at 23:43
    
@Theta30: Guess the UI text has changed since the version I used. –  Gnubie Apr 2 at 17:56
add comment

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.

share|improve this answer
    
Thanks man :) but i allready used my code, but that can be very helpfull for others. Btw maybe i'll wrote a Notepad++ plugin for that. which langauge they use in notepad++ plugins ? –  Danpe Sep 2 '11 at 13:17
    
@Danpe most Notepad++ plugins are written in C++, but technically can be extended to any other language capable of compiling a .DLL (and with the proper API bindings). –  Breakthrough Sep 2 '11 at 13:44
    
So I think you just created a plugin :) –  Danpe Sep 2 '11 at 13:59
add comment

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
share|improve this answer
    
I am on Windows 7, Where do i paste that code? this is really intresting. –  Danpe Sep 2 '11 at 13:58
1  
Open the start menu and just type Powershell. Double click "Windows Powershell". It will open a command prompt. Run it there. There is also and IDE (although they call it an ISE - Integrated Script Editor), but I do most of my work at the command prompt and I use GVim for script editing. –  EBGreen Sep 2 '11 at 14:04
add comment

here is a 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);
        }
    }
}
share|improve this answer
    
Why do you need the unsafe keyword on Main here? –  Breakthrough Sep 6 '11 at 19:05
    
@Break I dont know how it got there :O lol fixed. –  Danpe Sep 7 '11 at 15:31
add comment

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
share|improve this answer
1  
well if you are going to use excel anyway, just copy the file contents from any editor, paste to excel. Fill series the next column over with a series of numbers. Select both columns and sort descending on the number column. Copy the text column and paste back to the editor. –  EBGreen Sep 2 '11 at 15:15
    
@EBGreen, you are correct. I'm an idiot. You just need to be careful because Excel will change your data (messes with numbers, dates and such). –  Dale Sep 5 '11 at 12:27
add comment

You can do it online on the website http://textmechanic.com/Sort-Text-Lines.html

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.