View Full Version : Bash: display line numbers of blank lines
Dear all,
Is there a single line way of using bash to take a file "filename" and display the line numbers of any blank lines within that file?
Thanks,
Natski
Is this homework? If so, do you need to do this using bash primitives only, or can you use any of the standard unix utilities?
No it's not homework, sadly I'm way too old for that... I'm just writing one of my first bash scripts and this was one problem I came across which I could find in any books or forums.
I have started writing a separate script to do it but it is quite long and I think there should be a quicker one line method.
Natski
If you don't want the colons that grep prints,
grep -n '^$' file.name | sed -e 's/://'
A line that contains spaces (only) looks exactly like a blank line. You might want to count those, too:
grep -n '^ *$' file.name | sed -e 's/://'
The point of all this: Why use bash (or tcsh, or whateversh) primitives when you have the full power of the unix utilities at your hand?
Thanks for the help. I don't really understand what you mean. Isn't bash the default shell of UNIX? How can UNIX do anything without a shell, like Bash? It sounds like you're suggesting UNIX has its own intrinsic methods of doing these things but I don't see how that's possible unless you speak binary.
natski
vBulletin® v3.7.6, Copyright ©2000-2009, Jelsoft Enterprises Ltd.