No newline at end of file

Hello all,

I have maybe a simple Problem - but I do not know to handle it.

All what I want, is to write a line to file without a newline at the end. It works with "echo -n" for all lines, but not for the last one. At the end of the file is always a "0a" (hex)

My small script:

a_string="Te{t"


printf "%s" $a_string >> /home//scripts/tmp/t.out

echo -n $a_string >> /home/scripts/tmp/t2.out

I tried it in These both ways, but everytime I get this:

0000000: 5465 7b74 0a                             Te{t.

What can I do to avoid the newline-character at end of file?

What operating system are you using?

What shell are you using?

When I execute the script:

a_string="Te{t"


printf "%s" $a_string >> t.out

echo -n $a_string >> t2.out

on macOS High Sierra with either bash or ksh , I get the following output in your two output files:

$ od -bc t.out
0000000   124 145 173 164                                                
           T   e   {   t                                                
0000004
$ od -bc t2.out
0000000   124 145 173 164                                                
           T   e   {   t                                                
0000004
$

In addition to what Don Cragun said: Which exact command die you use to get the hex dump output?

Hello all,

I am using: RedHat Linux 3.10.0-862.el7.x86_64

I am working under: BASH_VERSION='4.2.46(2)-release'

The hex Editor is from vim (:%!xxd).

------ Post updated at 02:05 PM ------

Hello again,

if I do it with "od"-command - I will also get a result as Don Cragun described:

0000000 124 145 173 164
          T   e   {   t
0000004

------ Post updated at 02:06 PM ------

So, why are there different Outputs? (od and vim)?

I am not a vim developer so i can only speculate: vim - like vi - is not a hex-editor, but a text-editor. As such it (tries to) create(s) "well-formed" text files. A text file in UNIX has to have a newline character at the end of each line - if there is only one line, like in your case, then it has to be at the end, before the EOF-character.

I suppose that od is showing the file as it is (well, this in fact i know) and vim is "correcting" the file format as it opens it. I bet that when you save this to a different file and analyse that with od it will show the "extra" newline character the same way as vim does.

I hope this helps.

bakunin

1 Like

@bakunin: Yes - this helps a lot. Thats means I have no Chance to see a file without a newline character in vim. With 'od' I will see the real world - as it is.

Thanks for that good Explanation.

You can configure vim to not add the newline at the end. Unless your vim is already quite old, try to put into .vimrc a

set nofixendofline

Whether this is a wise choice, I'm not sure. In general, I do want my text editor to add the final newline, but this is up to you to decide.

1 Like

Thanks for this hint. As you wrote it it maybe better to add the final newline within the Editor... A question I have to think about...

Yes, because a missing newline is a violation in Unix, and cause problems with many text processing tools.

Because we're dealing with 2 different things here...one is the kernel view of the file which is an unformatted stream of bytes...and another is the application's view of the file. Some applications view the file as lines of text delimited by newlines, while others view it as fixed length records. The syntax of accessing the data in a file is defined by the kernel, which is an unformatted stream of bytes, and this syntax is identical for all programs, but the semantics or interpretation of the data varies from program to program...

On the other hand, a file without newlines containing printer escape sequences can be prefixed to a (plain text) print file without adding lines to the file and changing the page format.