Gilles 'SO- stop being evil' k gold badges silver badges bronze badges. Steven D Steven D Hm is the last piece really line? I'm sure it depends on everyone's usecase; for the 'last piece' is usually a line of text that someone didn't cap off with a newline. The usecase I most often encounter is a file with a single string of text that does not end in a newline.
That is, without a newline at the end, you don't have a line and strictly speaking, not a valid text file. This is a very inefficient way to do it. Remember, bash reads are slow. I just tried with a text file of MB taking an actual text file and concatenating it a thousand times and more than 2. Not nothing — the tools do it in a fraction of a second — but certainly awaitable.
This would miscount if any line ended with a backslash. Paolo Paolo 31 2 2 bronze badges. If you're looking to count smaller files a simple wc -l file.
Henry Tseng Henry Tseng 3 3 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back How do I count the total number of lines in the file? There are several ways to count lines in a file. The wc utility displays the number of lines, words, and bytes contained in each input file, or standard input if no file is specified to the standard output.
You can have any command here instead of cat. Output from any command can be piped to wc command to count the lines in the output. Our good old friend "grep" can also be used to count the number of lines in a file. Save Article. Like Article. Last Updated : 18 May, Using wc command. Using method 1. Using method 2. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert.
Writing code in comment? Please use ide. Load Comments.
0コメント