No newline at end of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting No newline at end of file
# 1  
Old 09-25-2018
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:

Code:
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:


Code:
0000000: 5465 7b74 0a                             Te{t.

What can I do to avoid the newline-character at end of file?
# 2  
Old 09-25-2018
What operating system are you using?

What shell are you using?

When I execute the script:
Code:
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:
Code:
$ 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
$

# 3  
Old 09-25-2018
In addition to what Don Cragun said: Which exact command die you use to get the hex dump output?
# 4  
Old 09-25-2018
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:


Code:
0000000 124 145 173 164
          T   e   {   t
0000004

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

So, why are there different Outputs? (od and vim)?
# 5  
Old 09-25-2018
Quote:
Originally Posted by API
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
This User Gave Thanks to bakunin For This Post:
# 6  
Old 09-25-2018
@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.
# 7  
Old 09-26-2018
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

Code:
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.
This User Gave Thanks to rovf For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Newline in the file

I have requirement to remove the /n ( newline ) characters from the file. When I open file in VI .. I want to see newline char how to display newline char .. or where can I see the content with newline char visible? (3 Replies)
Discussion started by: freakabhi
3 Replies

2. Shell Programming and Scripting

Warning while sorting : A newline character was added to the end of file

Hi, I am trying to sort a csv file which has say 10 lines each line having a row size that is upto 30183 no. of COLUMNS (Row length = 30183). There is a LINE FEED (LF) at the end of each line. When I try to sort this file say, based on the second FIELD using the below command, sort -t ',' +1... (5 Replies)
Discussion started by: DHeisenberg
5 Replies

3. Shell Programming and Scripting

Appending newline character End of File

Hi Gurus, Need help. I'm a beginner in Unix. I have a requirement, need to add or append newline (\n) character in file. Sample Data: 1|Main|Test|~# 2|Main|Hello|~# 3|Main|Unix|~# 4|Main|File|~#Output: 1|Main|Test|~# 2|Main|Hello|~# 3|Main|Unix|~# 4|Main|File|~#\n -- append only... (13 Replies)
Discussion started by: Gouri Solleti
13 Replies

4. Shell Programming and Scripting

How to add newline character at end of file?

Hi All, I have following piece of code in UNIX C Shell script and I want to add one more command which can add newline at the end of file only if there is no newline character exists. foreach file (`ls $dd_PLAYCARD_EDI_IN`) if ( -f $dd_PLAYCARD_EDI_IN/${file} ) then cat -n... (4 Replies)
Discussion started by: jnrohit2k
4 Replies

5. Shell Programming and Scripting

Newline characters in fields of a file

My source file is pipe delimeted file with 53 fields.In 33 rd column i am getting mutlple new line characters,dule to that record is breaking into multiple records. Note : here record delimter also \n sample Source file with 6 fields : 1234|abc| \nabcd \n bvd \n cde \n |678|890|900\n ... (6 Replies)
Discussion started by: lakshmi001
6 Replies

6. Shell Programming and Scripting

How to enter a newline after every XML tag end?

Hi Guyz, I have an XML message in following format: I want my contents to be formatted in following order: i.e. I want a newline after every XML tag end. How to do this? Thnx in advance. (5 Replies)
Discussion started by: DTechBuddy
5 Replies

7. Shell Programming and Scripting

Add end of char \n on end of file

Hi, I want to add \n as a EOF at the end of file if it does't exist in a single command. How to do this? when I use command echo "1\n" > a.txt and od -c a.txt 0000000 1 \n \n 0000003 How does it differentiate \n and eof in this case? Regards, Venkat (1 Reply)
Discussion started by: svenkatareddy
1 Replies

8. Shell Programming and Scripting

How to remove a newline character at the end of filename

Hi All, I have named a file with current date,time and year as follows: month=`date | awk '{print $2}'` date=`date | awk '{print $3}'` year=`date | awk '{print $6}'` time=`date +%Hh_%Mm_%Ss'` filename="test_"$month"_"$date"_"$year"_"$time".txt" > $filename The file is created with a... (2 Replies)
Discussion started by: amio
2 Replies

9. Shell Programming and Scripting

Append newline at the file end

Hi All, Is there any way to append a newline character at the end of a file(coma-separated file), through shell script? I need to check whether newline character exists at the end of a file, if it does not then append it. Regards, Krishna (1 Reply)
Discussion started by: KrishnaSaran
1 Replies

10. UNIX for Dummies Questions & Answers

How to convert ^M appearing at end of line to unix newline?

How to convert ^M appearing at end of line to unix newline? As I have tried with ^M in 'tr' it replaced ^ to a newline. Thanks in advance. (21 Replies)
Discussion started by: videsh77
21 Replies
Login or Register to Ask a Question