Hot answers tagged

106

Here you go: sed -i -e '$a\' file And alternatively for OS X sed: sed -i '' -e '$a\' file This adds \n at the end of the file only if it doesn’t already end with a newline. So if you run it twice, it will not add another newline: $ cd "$(mktemp -d)" $ printf foo > test.txt $ sed -e '$a\' test.txt > test-with-eol.txt $ diff test* 1c1 < foo \ ...


88

It's not about adding an extra newline at the end of a file, it's about not removing the newline that should be there. A text file, under unix, consists of a series of lines, each of which ends with a newline character (\n). A file that is not empty and does not end with a newline is therefore not a text file. Utilities that are supposed to operate on text ...


25

Essentially "because it's been done that way since manual typewriters". Really. A manual typewriter had a carriage on which the paper was fed, and it moved forward as you typed (loading a spring), and had a lever or key which would release the carriage, letting the spring return the carriage to the left-margin. As electronic data entry (teletype, etc) ...


21

Not necessarily the reason, but a practical consequence of files not ending with a new line: Consider what would happen if you wanted to process several files using cat. For instance, if you wanted to find the word foo at the start of the line across 3 files: cat file1 file2 file3 | grep -e '^foo' If the first line in file3 starts with foo, but file2 ...


20

From AskUbuntu, answer by Gilles: If you see the error “: No such file or directory” (with nothing before the colon), it means that your shebang line has a carriage return at the end, presumably because it was edited under Windows (which uses CR,LF as a line separator). The CR character causes the cursor to move back to the beginning of the line after ...


19

Have a look: $ echo -n foo > foo $ cat foo foo$ $ echo "" >> foo $ cat foo foo so echo "" >> noeol-file should do the trick. (Or did you mean to ask for identifying these files and fixing them?) edit removed the "" from echo "" >> foo (see @yuyichao's comment) edit2 added the "" again (but see @Keith Thompson's comment)


19

a shorter and simpler sed solution: sed ' : again /\\$/ { N s/\\\n// t again } ' textfile or one-liner: sed ':x; /\\$/ { N; s/\\\n//; tx }' textfile


17

It is a bad idea (to have strange characters in file names) but you could do mv somefile.txt "foo bar" (you could also have done mv somefile.txt "$(printf "foo\nbar")" or mv somefile.txt foo$'\n'bar, etc... details are specific to your shell. I'm using zsh) Read more about globbing, e.g. glob(7). Details could be shell-specific. But understand that /...


15

If the goal is just to avoid affecting the timestamp, dos2unix has a -k or --keepdate option which will keep the timestamp the same. It will still have to do a write to make the temporary file and rename it, but your timestamps will not be affected. If any modification of the file is unacceptable, you can use the following solution from this answer. find . ...


15

Yes, this happens because it is a "partial line". And by default zsh goes to the next line to avoid covering it with the prompt. When a partial line is preserved, by default you will see an inverse+bold character at the end of the partial line: a "%" for a normal user or a "#" for root. If set, the shell parameter PROMPT_EOL_MARK can be used to ...


14

What echo does with character escapes is implementation defined. In many implementations of echo (including most modern ones), the string passed is not examined for escapes at all by default. With the echo provided by GNU bash (as a builtin), and some other echo variants, you can do something like the following: echo -en 'first line\nsecond line\nthird ...


13

You can use dos2unix as a filter and compare its output to the original file: dos2unix < myfile.txt | cmp -s - myfile.txt


13

sed -i ':a;N;$!ba;s/\n/,/g' test.txt From http://stackoverflow.com/questions/1251999/sed-how-can-i-replace-a-newline-n : create a label via :a append the current and next line to the pattern space via N if we are before the last line, branch to the created label $!ba ($! means not to do it on the last line (as there should be one final newline)). finally ...


13

This really is trivial in Perl, you shouldn't hate it! perl -i.bak -pe 's/>\n/>/' file Explanation -i : edit the file in place, and create a backup of the original called file.bak. If you don't want a backup, just use perl -i -pe instead. -pe : read the input file line by line and print each line after applying the script given as -e. s/>\n/>...


12

Those trailing newlines are added by nano, not by cat. Use nano's -L parameter: -L (--nonewlines) Don't add newlines to the ends of files. Or ~/.nanorc's nonewlines command: set/unset nonewlines Don't add newlines to the ends of files.


12

You can use unix2dos (which found on Debian): unix2dos file Note that this implementation won't insert a CR before every LF, only before those LFs that are not already preceded by one (and only one) CR and will skip binary files (those that contain byte values in the 0x0 -> 0x1f range other than LF, FF, TAB or CR). or use sed: CR=$(printf '\r') sed "s/\$...


12

If you use bash, this command should work. mv a $'b\nc'


12

Define a shell function which outputs an end of line after every file and use it instead of cat: endlcat() { for file in "$@"; do cat -- "$file" echo done } then you can use endlcat *. The for loop loops over all provided arguments ($@) which are already escaped by the shell when you use wildcards like *. The -- is required to not choke on ...


11

Here's an awk solution. If a line ends with a \, strip the backslash and print the line with no terminating newline; otherwise print the line with a terminating newline. awk '{if (sub(/\\$/,"")) printf "%s", $0; else print $0}' It's also not too bad in sed, though awk is obviously more readable.


11

What needs to be explained is that the command appeared to work, not its exit code '\n' is two characters: a backslash \ and a letter n. What you thought you needed was $'\n', which is a linefeed (but that wouldn't be right either, see below). The -d option does this: -d delim continue until the first character of DELIM is read, rather ...


11

Because GNU find doesn't support \n as an escape sequence. The regexp \n matches the character n. GNU find copies the traditional Emacs syntax, which doesn't have this feature either¹. While GNU find supports other regex syntax, none support backslash-letter or backslash-octal to denote control characters. You need to include the control character literally ...


11

You asked for using some syntax with the echo command: echo $'first line\nsecond line\nthirdline' > foo (But consider also the other answer you got.) The $'...' construct expands embedded ANSI escape sequences.


10

It is possibly easiest with perl (since perl is like sed and awk, I hope it is acceptable to you): perl -p -e 's/\\\n//'


10

That's why you don't use line-by-line utilities for this. $ tr '\n' ' ' < input.txt > output.txt


10

Converting a standalone file If you run the following command: $ dos2unix <file> The <file> will have all the ^M characters stripped. If you want to leave <file> intact, then simply run dos2unix like this: $ dos2unix -n <file> <newfile> Parsing output from a command If you need to do them as part of a chain of commands ...


10

sed 's/\\n/\ /g' Notice the backslash just before hitting return in the replacement string.


10

Use this: sed 's/|END|/\n/g' test.txt What you attempted doesn't work because sed uses basic regular expressions, and your sed implementation has a \| operator meaning “or” (a common extension to BRE), so what you wrote replaces (empty string or END or empty string) by a newline.


9

Another solution using ed. This solution only affect the last line and only if \n is missing: ed -s file <<< w It essentially works opening the file for editing through a script, the script is the single w command, that write the file back to disk. It is based on this sentence found in ed(1) man page: LIMITATIONS (...) If a ...


9

You can use tr, as in tr -d '\040\011\012\015', which will remove spaces, tabs, carriage returns and newlines.


9

There are two aspects: There are/were some C compilers that cannot parse the last line if it does not end with a newline. I guess that the C standard specifies that a C program have to end with a newline (perhaps because some vendor of such a compiler was part of the committee when the first standard was written). Thus the warning by GCC. diff programs (...



Only top voted, non community-wiki answers of a minimum length are eligible