I know it's solved but, I needed to output numbered lines while ignoring empty lines, so I thought of putting it right here in case someone needs it. This version is very fast compared to solutions that invoke external commands like sed and grep. Also it skips lines that contain only spaces, the lines don't need to be empty to be skipped. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?
Collectives on Stack Overflow. Learn more. Skip blank lines when iterating through file line by line Ask Question.
Asked 7 years, 10 months ago. Active 5 months ago. Viewed 61k times. Improve this question. Add a comment. Active Oldest Votes. Jordan Running Jordan Running How about:! Tripp Kinetics Tripp Kinetics 4, 2 2 gold badges 21 21 silver badges 35 35 bronze badges. Does it work? I would be surprised if it did, but don't have time to test. Good luck! Was missing the file name variable in there, but otherwise it works fine. Sign up or log in Sign up using Google. Sign up using Facebook.
Sign up using Email and Password. Post as a guest Name. So maybe not simpler, but an alternative. Andrew Barber More information about the square bracket notation can be found at the man page of test — c0dem4gnetic. Therefore the continue solution is always recommended. Ignacio Vazquez-Abrams Ignacio Vazquez-Abrams k gold badges silver badges bronze badges. I need smth that would work in both bash and sh. I wonder why. Willi Mentzel NickCoder 1, 2 2 gold badges 17 17 silver badges 30 30 bronze badges.
Rounak Rounak 1. Sign up or log in Sign up using Google. Improve this question. Matthias Braun Eduardo Eduardo 18k 20 20 gold badges 60 60 silver badges 72 72 bronze badges. Add a comment. Active Oldest Votes. You'll need tail. Improve this answer. SingleNegationElimination SingleNegationElimination k 30 30 gold badges silver badges bronze badges. JoelClark No, NickSoft is right. Show 3 more comments. David Parks David Parks In the more general case, you'd have to use sed 1,Xd where X is the number of initial lines to delete, with X greater than 1.
This makes more sense if you don't know how long the file is and don't want to tell tail to print the last lines. MERose 3, 7 7 gold badges 43 43 silver badges 73 73 bronze badges. Eddie Eddie Lloeki Awesome! Anyway, thanks. Peter Mortensen This version works in the Cygwin tools that come with Git for Windows, whereas tail and sed do not.
0コメント