Unix & Linux: How do I remove newline character at the end of file? (4 Solutions!!)
Channel: Roel Van de Paar
Date: 2020-08-19

Unix & Linux: How do I remove newline character at the end of file?


The Question: Let me clear about newline character:
$ echo Hello ＞ file1 ; cat file1
Hello
$ echo -n Hello ＞ file2 ; cat file2
Hello$
Here you can see that file1 has newline character at the end whereas file2
hasn't.
Now suppose I've one file:
$ cat file
Hello
Welcome to
Unix
$
And I want to add and Linux at the end of file, then echo " and Linux" ＞＞ file
will be added to newline. But I want last line as Unix and Linux
So, In order to work around I want to remove newline character at the end of
file. Therefore How do I remove the newline character at the end of file?

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 24 people ==
Though, you can remove newline character from line by tr -d 'n':
$ echo -e "Hello"
Hello
$ echo -e "Hello" | tr -d 'n'
Hello$
You can remove the newline character at the end of file using following easy
way:
 1. head -c -1 file
 From man head:
 -c, --bytes=[-]K
 print the first K bytes of each file; with the leading '-',
 print all but the last K bytes of each file
 2. truncate -s -1 file
 from man truncate:
 -s, --size=SIZE
 set or adjust the file size by SIZE
 SIZE is an integer and optional unit (example: 10M is 10*1024*1024).
 Units are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers
 of 1000).

 SIZE may also be prefixed by one of the following modifying characters:
 '+' extend by, '-' reduce by, '＜' at most, '＞' at least, '/' round down
 to multiple of, '%' round up to multiple of.

== This solution helped 3 people ==
Here's one way with sed -- on the last ($) line of the file, search and replace
anything and everything (.*) with "whatever you matched" followed by " and
Linux":
sed '$s/(.*)/1 and Linux/' file
An even simpler solution, courtesy of https://unix.stackexchange.com/users/
265604/isaac, is:
sed '$s/$/ and Linux/' file
This replaces the (symbolic) end-of-line with the given text.

== This solution helped 38 people ==
If all you want to do is add text to the last line, it's very easy with sed.
Replace $ (pattern matching at the end of the line) by the text you want to
add, only on lines in the range $ (which means the last line).
sed '$ s/$/ and Linux/' ＜file ＞file.new &&
mv file.new file
which on Linux can be shortened to
sed -i '$ s/$/ and Linux/' file
If you want to remove the last byte in a file, Linux (more precisely GNU
coreutils) offers the truncate command, which makes this very easy.
truncate -s -1 file
A POSIX way to do it is with dd. First determine the file length, then truncate
it to one byte less.
length=$(wc -c ＜file)
dd if=/dev/null of=file obs="$((length-1))" seek=1
Note that both of these unconditionally truncate the last byte of the file. You
may want to check that it's a newline first:
length=$(wc -c ＜file)
if [ "$length" -ne 0 ] && [ -z "$(tail -c -1 ＜file)" ]; then
# The file ends with a newline or null
dd if=/dev/null of=file obs="$((length-1))" seek=1
fi

With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user Stephane Chazelas (https://unix.stackexchange.com/users/22565), user Pandya (https://unix.stackexchange.com/users/66803), user Jeff Schaller (https://unix.stackexchange.com/users/117549), user Hi-Angel (https://unix.stackexchange.com/users/59928), user Gilles 'SO- stop being evil' (https://unix.stackexchange.com/users/885), and the Stack Exchange Network (http://unix.stackexchange.com/questions/254644). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.