Find longest line in a file


















Edward Edward Not enough to downvote, but I disagree. This is a problem on machines using an OS from Redmond. On other machines, are you going to find text files that are 2 billion lines long, lines that are 2 billion characters long? The answer is almost always no.

This depends on ones interpretation of a key vague point in the challenge, how to treat lines of the same length. Adding that shortcut was a travesty. Better, more descriptive naming is still advisable, regardless of the form it's expressed. I use Linux and gcc here; gcc correctly warns about a sign mismatch in the comparison. IMHO, it's best to address the potential problem during construction rather than have it fail in the distant future when someone does try it on 2 billion lines.

There are multiple arguments on all fronts. Shreevardhan Shreevardhan 6 6 bronze badges. The Overflow Blog. Podcast Helping communities build their own LTE networks. Podcast Making Agile work for data science.

Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related 4. Jens Jens There just is no need for useless cats in this world. Newbies should be taught accordingly right from the beginning. This should be rated higher, it's what the user asked for. Cairnarvon Anonymous Anonymous 1 1 silver badge 2 2 bronze badges. Thanks, I've been looking for a way to prevent wc from outputting the filename : — Peter.

Chris Koknat Chris Koknat 2, 2 2 gold badges 26 26 silver badges 29 29 bronze badges. Requires sorting a file.. Using a MB 6,, line text file as the source British National Corpus , the perl solution took 12 seconds, while wc -L took 3 seconds — Chris Koknat.

Paul Rooney There we go. That finds my obnoxiously long comments. Thanks dude. You could take this a step further and eliminate cat. Important overlooked point in the above examples. John Kearney John Kearney 3 3 silver badges 7 7 bronze badges. That second awk script will only tell you the longest length, not show the longest line.

Come on.. These are same as the first two answers added with the references. Jon Jon 1, 1 1 gold badge 20 20 silver badges 28 28 bronze badges. I am not sure this is a valid comparison, I would be worried that the awk version benefits from disk block caching of the wc version that is running first and seeds the disk cache.

You would have to randomize the order of who gets called first over the ten runs to make this argument stick.

Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Helping communities build their own LTE networks. Podcast Making Agile work for data science. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related Hot Network Questions. Get the maximum length number of a record in a file 1. Another freebie!! Just to recap, you can get I'll be happy to add an answer for you if you confirm one of the methods I listed works for you need, etc.

I just haven't dug into getting the line number but there's three method for getting that data from a file's records as explained. How many characters shall a tab count? Just 1, 4 or 8 or customizable? Show 2 more comments. Active Oldest Votes. I've listed two FREE solutions below that may suffice for your need. Improve this answer. Community Bot 1. Add a comment.



0コメント

  • 1000 / 1000