Tag Info

Hot answers tagged

79

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 \ ...


68

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 ...


16

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


16

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

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)


13

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 ...


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. ...


13

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 ...


12

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


11

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.


11

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 ...


11

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 ...


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

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.


10

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 ...


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 -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 ...


10

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 ...


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

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 ...


9

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


9

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


9

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: sed "s/$/$(printf '\r')/" ...


8

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 ...


8

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


7

The \ No newline at end of file you get from github appears at the end of a patch (in diff format, see the note at the end of the "Unified Format" section). Compilers don't care whether there is a newline or not at the end of a file, but git (and the diff/patch utilities) have to take those in account. There are many reasons for that. For example, ...


7

There is no end-of-file character in Unix or Linux filesystems. The read() system call returns 0 on end-of-file condition, if the file descriptor in use refers to a regular file. read() works differently on sockets and pipes. You don't get a special character to mark end of file. wc gave you 30 as a character or byte count because the first line has 12 ...


7

No idea why its there, but here's how to disable it with the GNU implementation of bc: echo '6^6^3' | BC_LINE_LENGTH=0 bc BC_LINE_LENGTH This should be an integer specifying the number of characters in an output line for numbers. This includes the backslash and newline characters for long numbers. As an extension, the value of ...


7

A perl solution: $ perl -pe 's/(?<=>)\n//' Explaination s/// is used for string substitution. (?<=>) is lookbehind pattern. \n matches newline. The whole pattern meanings removing all newline that have > before it.


7

Use recode, e.g.: recode /cr file Note: the fact that you can see the contents in the terminal with cat file is that the Mac end-of-line is CR, which puts the cursor at the beginning of the line without going to the next line, so that everything gets overwritten.



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