Bash: display line numbers of blank lines

In summary, the conversation discusses the use of bash to display the line numbers of any blank lines within a file. The suggested solution is to use the grep and sed utilities, as they provide a quicker and more efficient method. The conversation also touches on the role of shells in UNIX and the use of UNIX utilities.
  • #1
natski
267
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
  • #2
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
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
grep -n '^$' file.name
 
  • #5
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
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
 

Related to Bash: display line numbers of blank lines

What is Bash?

Bash is a command-line shell and scripting language used in many operating systems, including Linux, macOS, and Unix. It is the default shell for most Linux distributions and is used for executing commands and automating tasks.

How do I display line numbers in Bash?

To display line numbers in Bash, you can use the -n option with the cat command. For example, "cat -n file.txt" will display the contents of the file with line numbers. Another option is to use the nl command, which is specifically designed for adding line numbers to a file.

Can I display line numbers for blank lines in Bash?

Yes, you can display line numbers for blank lines in Bash by using the -b option with the cat command. This will number all lines, including blank ones. Alternatively, you can use the grep command with the -n option to search for blank lines and display their line numbers.

Is there a way to display line numbers only for specific lines in Bash?

Yes, you can use the sed command with the = operator to print the line number before the specified line. For example, "sed = file.txt | sed 'N;s/\n/ /'" will print the line number before each line in the file.

Can I save the output of line numbers to a file in Bash?

Yes, you can save the output of line numbers to a file in Bash by using the redirection operator ">". For example, "cat -n file.txt > output.txt" will save the output with line numbers to a file named output.txt. You can also use the tee command to display the output on the screen and save it to a file simultaneously.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
844
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
21
Views
2K
  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
4
Views
873
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top