# Bash: display line numbers of blank lines

1. Oct 18, 2009

### natski

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

2. Oct 18, 2009

### D H

Staff Emeritus
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?

3. Oct 18, 2009

### natski

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

4. Oct 18, 2009

### D H

Staff Emeritus
grep -n '^$' file.name 5. Oct 18, 2009 ### D H Staff Emeritus 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?

6. Oct 19, 2009

### natski

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