2

I am looking for a program that can find the longest line of a .txt file. It should run on Microsoft Windows. Any license or price is fine.

2
  • How many characters shall a tab count? Just 1, 4 or 8 or customizable? Commented Jan 23, 2017 at 10:18
  • @Walmart - I think that your answers above should be put into an answer rather than left as comments. Commented Jan 23, 2017 at 16:27

2 Answers 2

6

I am looking for a program that can find the longest line of a .txt file. It should run on Microsoft Windows.

I've listed two free solutions below that may suffice for your need.

Method 1

You could use: wc -L "c:\folder\file.txt" from the CoreUtils for Windows for example if you just need the number of the length of the longest line in that file—this is a freebie too.


Method 2

You can do this in Windows with PowerShell as well for a particular file:

Note: The -ReadCount 3000 parameter reads the file in chunks of 3000 lines, reducing memory usage for large files. Adjust the value based on your system's performance and file size for optimal results.

  1. Get the maximum length number of a record in a file—not optimized for large files:
Get-Content "c:\folder\file.txt" | Measure-Object -Property length -Maximum | Select-Object Maximum;
  1. Get the maximum length number of a record in a file—optimized for large files:
$maxLength = 0;
Get-Content "c:\folder\file.txt" -ReadCount 3000 | ForEach-Object {
    $_ | ForEach-Object {
        $length = $_.Length;
        if ($length -gt $maxLength) {
            $maxLength = $length;
        }
    }
};
$maxLength;
  1. Get the content of the longest line/record in a file—not optimized for large files:
Get-Content "c:\folder\file.txt" | Sort-Object -Property length | Select-Object -last 1;
  1. Get the content of the longest line/record in a file—optimized for large files:
$maxLine = ""
Get-Content "c:\folder\file.txt" -ReadCount 3000 | ForEach-Object {
    $_ | Sort-Object -Property length | Select-Object -Last 1 | ForEach-Object {
        if ($_.Length -gt $maxLine.Length) {
            $maxLine = $_;
        }
    }
};
$maxLine;

Further Resources

5
  • 1
    I would like to mention that if the file contains CTL language scripts like Devanagari, then use following command: GC "c:\folder\file.txt" -Encoding UTF8 | Sort -Property length | Select -last 1
    – ePandit
    Commented Sep 30, 2017 at 16:27
  • grep (also available in GnuWin) is incredibly useful to use in tandem with wc, once you have a threshold to search lines for. wc -L *.txt gets the maximum lines for any txt files in directory. then grep ".\{90\}" *.txt will print all lines in all txt files that are above 90. The powershell options you suggested are extremely slow on very large text files, while these are very fast by comparison.
    – bryc
    Commented Oct 6 at 1:40
  • @bryc .... I wrote this answer long ago, for large files and using Get-Content while constrained to PowerShell, you may have to incorporate in the -ReadCount parameter so perhaps -ReadCount 500 parameter to define the batch size for line processing. Test accordingly for your need if that's what you are trying to accomplish. Commented Oct 6 at 14:21
  • The answer date is irrelevant, as people may still find this page as I did. I am merely providing what worked for me (grep + wc) for others in the same boat because that PowerShell command can't handle a 5 million line text file (i changed -last 1 to -last 100 and it took ~9 mins and ate 9GB of RAM)
    – bryc
    Commented Oct 7 at 11:27
  • @bryc Answer edited and adjusted accordingly for memory efficient logic more optimized for extremely large files to help with hogging up RAM. Thanks for confirming the suggested wc option works with wildcard character for file names. Commented Oct 8 at 0:57
1

You can add an extension to Notepad++ to perform this operation. See this answer on SuperUser for details

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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