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.
-
How many characters shall a tab count? Just 1, 4 or 8 or customizable?– Thomas WellerCommented Jan 23, 2017 at 10:18
-
@Walmart - I think that your answers above should be put into an answer rather than left as comments.– Steve BarnesCommented Jan 23, 2017 at 16:27
2 Answers
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.
- 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;
- 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;
- 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;
- 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
-
1I 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– ePanditCommented Sep 30, 2017 at 16:27
-
grep
(also available in GnuWin) is incredibly useful to use in tandem withwc
, once you have a threshold to search lines for.wc -L *.txt
gets the maximum lines for any txt files in directory. thengrep ".\{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.– brycCommented 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)– brycCommented 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
You can add an extension to Notepad++
to perform this operation. See this answer on SuperUser for details