0

I made a mistake in my .editorconfig definition and forgot to add

insert_final_newline = true

So now some files do have a newline at the end while some do not.

I found similar questions such as:

However the solutions provided there add a newline to all files. I need to only add it to those that don't have a final newline already.

EDIT: The question that was linked as answering mine question doesn't do so as the answers are for single file only. I need this recursively for all files. Voting to reopen.

enumag
  • 830
  • 11
  • 21
  • BTW: I posted an answer, BUT I now see that the first answer you linked to, also solves the problem (in a slightly different way). – user1934428 Nov 09 '22 at 14:14
  • It doesn't. As I noted it adds a newline to all files, not just the ones missing it. – enumag Nov 10 '22 at 07:19
  • This is simply not true. I just tried it: See [here](https://user.fm/files/v2-cee1a86777e38268432cdd09b515a9eb/capture_20221110_092847.jpg). If it does not work for you, I would be curious to see a screenshot which demonstrates your point. – user1934428 Nov 10 '22 at 08:34
  • Which command are you using exactly? I tried multiple from the linked questions but all had the issue I described. – enumag Nov 10 '22 at 10:18
  • Did you have a look at the sceenshot which I posted? It shows the command I used. – user1934428 Nov 14 '22 at 08:18

1 Answers1

2

UPDATE : I updated this solution to accomodate the comment by Paul Hodges:

Just test, whether the last character in a file is a newline:

if [[ -z $(tail -c 1 YOUR_FILE) && ! -s YOUR_FILE ]]
then
  # File ends in a newline, don't add one
else
  # No newline in file, or file empty - add a newline
fi

Explanation:

If a file ends in a newline, tail -c 1 will return this newline character, but command substition will remove it. Therefore $(tail -c 1 ...) will be empty. However, if the file itself is empty, the $(tail ...) will also be empty. Therefore we need a check for the file being not-empty (! -s ... ).

user1934428
  • 19,864
  • 7
  • 42
  • 87
  • This was my first thought too, but in Git Bash it isn't working as I expected. – Paul Hodges Nov 09 '22 at 14:17
  • 2
    "isn't working" is not an accurate error description. It should work in any bash implementation new enough to support `$'...'`, and where you have `tail` in your path - both conditions are easy to check, so if you still encounter problems, you need to provide debugging details. – user1934428 Nov 09 '22 at 14:19
  • https://stackoverflow.com/a/10082466/8656552 addresses it. – Paul Hodges Nov 09 '22 at 15:36
  • Both this and the linked answer only solve the case of single file. How do I use this for all files? – enumag Nov 10 '22 at 07:21
  • 1
    @enumag : You write a loop which goes over all files. If you don't know how to write a loop in bash, please ask a new question, because on Stackoverflow, each question should focus on exactly one programming problem. – user1934428 Nov 10 '22 at 08:35
  • I'd rather use a one liner than a script though. – enumag Nov 14 '22 at 08:02
  • You can write the whole script into a single line. Just separate the statements by semicolon instead of a newline. – user1934428 Nov 14 '22 at 08:17
  • @PaulHodges: Thanks for pointing this out. I have updated my answer. Could you check it for correctness? – user1934428 Nov 14 '22 at 10:39