Unix file lines count
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. These examples are just to let you know that there are multiple ways to count the lines without using "wc -l". But if asked I will always use "wc -l" instead of these options as it is way too easy to remember.
Along with the above commands, its good to know some rarely used commands to find the line count in a file. Use the nl command line numbering filter to get each line numbered.
The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Asked 11 years, 1 month ago. Active 9 months ago. Viewed k times. I'm sure there are many ways to do this: how can I count the number of lines in a text file? Improve this question. Chris Smith Chris Smith 1 1 gold badge 6 6 silver badges 6 6 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Michael Mrozek Michael Mrozek How do I count the lines in a file if I want to ignore comments?
I tried doing this with grep, but couldn't figure out the correct regular expression. Gdalya I hope the following pipeline will do this no tests were perfomed : cat matlab. Probably instead of matching only spaces, you'd want to match arbitrary whitespace; for this replace the space with [[:space:]].
Dennis Williamson Dennis Williamson 6, 1 1 gold badge 29 29 silver badges 36 36 bronze badges. Is any of these useful in itself as opposed to things to build upon to make a program that does more than counting lines , other than wc -l and pure ba sh? Gilles: I think the phrase "many ways" in the question triggered a challenge that Steve and I rose to. Gilles: Oh, you meant first. Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.
Save my name, email, and website in this browser for the next time I comment. Notify me of followup comments via e-mail. You can also subscribe without commenting. This site uses Akismet to reduce spam. Learn how your comment data is processed. Display Number of Words. If you liked this article, then do subscribe to email alerts for Linux tutorials.
0コメント