1
cat -b test1.txt

This command is meant to skip blank lines and number the rest.

cat -n test1.txt

This command numbers lines no matter what.

I tried both of them. One on a file with only spaces and tabs and other with some alphabets. Both of them numbered both the files identically. Is filling a file with spaces and tabs a way to create an empty line or am I doing it wrong?

Update

I don't know what is even unclear in this. So here is how I reached http://askubuntu.com

I was reading I was reading the book Linux Command Line and Shell Scripting Bible by Richard Blum. It had mentioned this:

enter image description here

So I tried to do that. I thought empty lines should only contain tabs or spaces. So I created a file with tabs and spaces (in order to have an empty line). So I tried running this command:

cat -b test1.txt

However, it still numbered the lines which contained only tabs and spaces. So I knew that tabs and spaces were still being counted as characters. I asked a question here for how to add a blank line in a text file for the code:

cat -b text1.txt

to work.

This is the best I can do to explain my question.

closed as unclear what you're asking by αғsнιη, waltinator, David Foerster, Eric Carvalho, user364819 Sep 18 '17 at 21:46

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Now the question is unclear. – Pilot6 Sep 17 '17 at 12:03
  • 1
    Now it is even more unclear. You never mention what you want to achieve. You tried some commands, didn't like the output for some reason. The last sentence is 100% unclear. – Pilot6 Sep 17 '17 at 12:42
  • 1
    At least you tried, but it's still very unclear what you're up to. Does my answer below help? If not, please explain what an empty line means for you and where you want it to appear. – dessert Sep 17 '17 at 12:50
  • 1
    Line with Tabs and Spaces isn't empty. An empty line contains nothing. – Pilot6 Sep 17 '17 at 12:56
  • 1
    @Pilot6 And that's what's actually and finally answers OP's question – add it to your answer! – dessert Sep 17 '17 at 13:10
6

You can add an empty line to a file called "file_name" by running

echo >> file_name

An empty line is a line that contains literally nothing including tabs or spaces.

5

Adding blank lines in a text file can be done by:

  • Opening the text file in an editor
  • Press the Enter key
  • Save the file
  • Will not the enter key still be considered as a character? – Reeshabh Ranjan Sep 17 '17 at 12:58
  • 1
    This is also a good way to add an empty line. – Pilot6 Sep 17 '17 at 13:13
1

To have a blank line inserted in your output, just first output the first part of your file, then run echo and then output the second part.

E.g. for a file test:

> cat test
line 1
line 2
line 3
line 4
> head -2 test && echo && tail -n+3 test
line 1
line 2

line 3
line 4

You could define a function to automate this, something like:

> blank_line(){ head -$(bc <<< $1-1) $2 && echo && tail -n+$1 $2;}
> blank_line 2 test
line 1

line 2
line 3
line 4

As you can see, this simple function takes the line number where the blank line should be displayed as the first and the filename as the second argument.

Not the answer you're looking for? Browse other questions tagged or ask your own question.