Tag Info

Hot answers tagged

51

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


45

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


14

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

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

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


7

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


7

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


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

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


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


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


5

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


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


5

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


5

The only thing I'm aware of that commonly used a bare CR as a line terminator is old Mac systems (before Mac OS X) but unless it's a really old file that seems unlikely. In any case the mac2unix program in the dos2unix package should be able to fix it for you.


5

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


5

This should work: printf "one\ntwo\n" | awk 'NR>1{print PREV} {PREV=$0} END{printf("%s",$0)}' ; echo " done" The script always prints previous line instead of current, and the last line is treated differently. What it does in more detail: NR>1{print PREV} Print previous line (except the first time). {PREV=$0} Stores current line in PREV variable. ...


4

No. There is nothing special about a binary or non-binary file. You can use heuristics like 'contains only characters in 0x01–0x7F', but that'll call text files with non-ASCII characters binary files, and unlucky binary files text files. Now, once you've ignored that... zip files If its coming from your Windows user as a zip file, the zip format supports ...


4

echo appends a newline, unless you tell it not to, by putting -n first or \c at the end, or putting -e first and \c at the end or... you really don't want to know all the varieties of echo. Use printf moo > cow and you'll have a file with zero lines.


4

That's MSDOS charset. Try recode cp437..u8 in a UTF8 terminal. It gives: ██▀▀▀▀▀▀ ██▀▀▀▀▀█ █▀▀▀▀▀█ ██▀▀█▀▀█ ██ █▀▀▀▀▀█ ██▀▀█ ██ ██▀▀▀▀▀▄ ██▄▄▄▄▄▄ ██▄▄▄▄▄█ █▄▄▄▄▄█ ██ ██ ██ ██ █▄▄▄▄▄█ ██ ██ ██ ██ ██ ▄█ ██ █ ▄█ ██ ██ ██ █ ▄█ ██ ██ ██ ██ ██ ▄▄▄▄▄▄▄█ ██ █ █ ██ ██ ██▄▄▄▄▄ █ █ ██ ██▄▄█ ██▄▄▄▄▄▀ ...



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