PDA

View Full Version : Bash: display line numbers of blank lines


natski
Oct18-09, 05:57 PM
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

D H
Oct18-09, 06:10 PM
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?

natski
Oct18-09, 07:15 PM
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

D H
Oct18-09, 08:34 PM
grep -n '^$' file.name

D H
Oct18-09, 08:41 PM
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?

natski
Oct19-09, 05:19 PM
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