Tag Info

Hot answers tagged

62

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


50

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


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

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


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.


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


9

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


9

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


9

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


9

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


8

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


8

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


8

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


8

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')/" ...


7

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


7

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


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.


7

You should be able to use sed -e :a -e '/\\$/N; s/\\\n//; ta' See Peter Krumins' Famous Sed One-Liners Explained, Part I, 39. Append a line to the next if it ends with a backslash "\".


7

The problem with the script is that when copying a file from another system like Windows, it adds a newline \n and a carriage return \r\n. For more about line feeds see newline entry on wikipedia. To demonstrate the issue I've uploaded short fragment here which explains how to solve the problem. In short: Use tr to remove those weird line endings tr -d ...


7

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


6

I'd use file and pipe the output into grep to find text files, then extract just the filename portion of file's output and pipe that into xargs. something like: file * | grep 'ASCII text' | awk -F: '{print $1}' | xargs -d'\n' -r flip -u Note that the grep searches for 'ASCII text' rather than any just 'text' - you probably don't want to mess with Rich ...


6

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


6

Executing with #!/bin/bash -x shows: + echo Stock List SXX.L $'27.50\r' QPP.L $'14.2495\r' Those \r will likely be what's messing up the output. It moves the cursor back to the beginning of the line, and whatever follows afterward overwrites what's already there.


6

You are using OSX 10.8, so your sed implement is FreeBSD sed, not GNU sed. So FreeBSD sed does not interpret \n as newline. You can try: $ sed 's/ /\'$'\n/g' myfile $'\n/g' causes bash to interpret the string as standard escape expansion, so \n is converted to newline before sed process. or using tr instead of sed: $ tr -s ' ' '\n' < myfile


6

Well, I can think of a couple of simple ways but neither involves grep (which doesn't do substitutions anyway) or sed. Perl To replace each occurrence of "line"\n<second> with other characters, use: $ perl -00pe 's/"line"\n<second>/other characters /g' file first other characters line and so on Or, to treat multiple, consecutive occurrences ...


6

read the whole file and do a global replacement: sed -n 'H; ${x; s/"line"\n<second>/other characters /g; p}' <<END first "line" <second> line followed by "line" <second> and last END first other characters line followed by other characters and last


5

You're better off correcting the editor of the user who last edited the file. If you are the last person to have edited the file - what editor are you using, I'm guessing textmate ..?



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