0

I have a UTF-8/no BOM file (converted from ISO-8859-1) that has 31214 lines. I have already run dos2unix on the file. When I open it in notepad++, I see a blank line underneath. When I remove this blank line, the line count reduces by one. I save it under a different name and when I tail the file, the prompt displays on the same line. From bash, how do I delete the blank line in the 1st file to produce the result displayed below in the 2nd file?

The goal is to do this from bash w/o manually deleting the line in notepad++

1st file:

[user@server]$ cat file1.txt | wc -l
31214
[user@server]$ tail file1.txt
T       31212           Data     20170517
[user@server]$

2nd file (edited with notepad++)

[user@server]$ cat file2.txt | wc -l
31213
[user@server]$ tail file2.txt
T       31212           Data     20170517[user@server]$ 
ilkkachu
  • 6,221
  • 16
  • 30
Goiko74
  • 1
  • 1

3 Answers3

1

That's the trailing newline of the last line. Some editors allow you to go to the nonexisting "empty" line at the end, some don't show it. Again, some programs may allow you to remove the final newline, but note that e.g. POSIX in effect requires it to be there, and some standard utilities act oddly if it isn't present.

E.g. wc -l counts the number of newlines in the input file (printf "foo\nbar" | wc -l shows 1) so removing the final newline does decrease the line count.

Also, Bash prints the prompt wherever it was that the cursor was left on the screen, so if you print something that doesn't have the trailing newline, the prompt will be placed where the final incomplete line ended, as you saw.

There's no need to remove that final newline, just leave it there.

ilkkachu
  • 6,221
  • 16
  • 30
  • Actually, the program that reads the text files derps because there is a newline at the end. I read that using `truncate -s -1 ` will produce the result I need. I will let you know if this works. Thanks for telling me about newlines – Goiko74 May 18 '17 at 17:02
  • removing the newline at the end of the file ended up fixing my issue – Goiko74 May 24 '17 at 14:20
0

To remove the final newline character it is possible, as explained here, to use

sed -i '$ s/.$//' your.file

which will substitute nothing for the last character in the last line of the file (if you want to delete smth else from the end of the file you can replace the regex .$ with smth-else$). -i means ‘substitute in-place’ (in FreeBSD/MacOS you need to add an empty string as an argument: sed -i "" '$ s/.$//' your.file)

Community
  • 1
  • 1
macleginn
  • 321
  • 2
  • 10
0

The file2.txt is missing a trailing newline.
Yes, a text file should end on a newline character.

Given that you do know that a trailing newline is missing, this command should be enough to correct the problem:

$ echo >> file2.txt