Use ex
, which is specified by POSIX. Add a tab-newline sequence at the start of the file, then run ex
's join
command.
This works whether the file is empty or not, and should be fully portable to any UNIX or Linux system.
(Note that if the file begins as an empty file, the contents after this command will be a tab character and a newline. A file without a terminating newline is technically not actually a text file.)
printf '0a\n\t\n.\n1j!\nx\n' | ex myfile.txt
If you run the printf
command by itself you can see the instructions that are getting passed to ex
:
$ printf '0a\n\t\n.\n1j!\nx\n'
0a
.
1j!
x
Note that there is a single tab character in the otherwise blank line above.