Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Bash: display line numbers of blank lines

  1. Oct 18, 2009 #1
    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. jcsd
  3. Oct 18, 2009 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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?
     
  4. Oct 18, 2009 #3
    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
     
  5. Oct 18, 2009 #4

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    grep -n '^$' file.name
     
  6. Oct 18, 2009 #5

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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?
     
  7. Oct 19, 2009 #6
    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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Bash: display line numbers of blank lines
Loading...