Bash: display line numbers of blank lines

Click For Summary

Discussion Overview

The discussion revolves around finding a method in bash to display the line numbers of blank lines in a specified file. Participants explore potential solutions and clarify the use of bash versus other Unix utilities.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Natski inquires about a one-liner in bash to display line numbers of blank lines in a file.
  • One participant questions whether the inquiry is related to homework and asks about the constraints on using bash primitives versus standard Unix utilities.
  • Natski clarifies that this is not homework but a personal project involving bash scripting.
  • Another participant suggests using the command grep -n '^$' file.name to find blank lines.
  • A follow-up response provides an alternative command to remove colons from the output and suggests considering lines that contain only spaces as blank lines, proposing grep -n '^ *$' file.name | sed -e 's/://'.
  • Natski expresses confusion about the suggestion to use Unix utilities instead of bash, questioning the role of the shell in executing commands.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the best approach to the problem, and there is some disagreement regarding the use of bash versus Unix utilities.

Contextual Notes

There is an implicit assumption that the definition of a "blank line" may vary, as some participants suggest including lines with only spaces. The discussion does not resolve the question of whether to use bash or other utilities.

natski
Messages
262
Reaction score
2
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
 
Technology news on Phys.org
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
 
grep -n '^$' file.name
 
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
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
10
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
33
Views
3K
Replies
12
Views
2K