Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find N longest lines in a text file and print them to stdout?

The first line contains the value of the number 'N' followed by multiple lines. I could solve it in order of n^2 algorithm. Can someone suggest a better one?

like image 375
rkt Avatar asked May 11 '11 06:05

rkt


People also ask

How do I print the longest line of a file?

Using the awk Command$ awk '{ln=length} ln>max{delete result; max=ln} ln==max{result[NR]=$0} END{for(i in result) print result[i] }' lines. txt Recently I have to process many text files. Sometimes files could have very long lines. For example, this is a really long...

Which command will print the length of the longest line?

-L: The 'wc' command allow an argument -L, it can be used to print out the length of longest (number of characters) line in a file.

How do you find the longest line in a file in Linux?

Print Longest Line in a File Using Using wc and grep Commands. By combining these two commands, you get to use regex from the grep command and max-line-length from wc command. The wc command takes the -L command option to determine max-line-length as demonstrated below.

How do you find the longest line in a file in Python?

To find the longest line in a text file using Python, open the file using the open() function and read the file content in a list of lines using file. readlines() function. Then call the max(lines, key=len) function on the list of lines using the length function as a key to find the longest line.


1 Answers

  1. You can use a minimum-heap and do it in O(n*(log(N))):

       heap = new Min-Heap(N)
       foreach line in text:
            if length(line) > heap.min():
            heap.pop()
            heap.insert(line)
       foreach line in heap:
            print to stdout: line.
    
  2. it could also be done in O(n) using Select(N) (which selects the Nth number) followed by partition around the Nth number (which arranges all the with size larger or equal to the Nth number to one side of it).

       i = Select(lines, N)
       partition(lines, i)
       for i to size(lines):
             print to stdout: lines[i]
    
like image 156
Gal Avatar answered Nov 03 '22 01:11

Gal