Tag Info

Hot answers tagged

42

Here you go: 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 \ No newline at end of file --- > foo $ echo $? 1 $ sed ...


41

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


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

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)


9

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

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

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

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

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


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


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.


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

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


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: ██▀▀▀▀▀▀ ██▀▀▀▀▀█ █▀▀▀▀▀█ ██▀▀█▀▀█ ██ █▀▀▀▀▀█ ██▀▀█ ██ ██▀▀▀▀▀▄ ██▄▄▄▄▄▄ ██▄▄▄▄▄█ █▄▄▄▄▄█ ██ ██ ██ ██ █▄▄▄▄▄█ ██ ██ ██ ██ ██ ▄█ ██ █ ▄█ ██ ██ ██ █ ▄█ ██ ██ ██ ██ ██ ▄▄▄▄▄▄▄█ ██ █ █ ██ ██ ██▄▄▄▄▄ █ █ ██ ██▄▄█ ██▄▄▄▄▄▀ ...


4

A line is a sequence of characters terminated by a newline character. The characters that appear after the last newline of a file are not part of a line. Such a file that has characters after the last newline is not a text file as per the POSIX definition of a text file, and the behaviour of text utilities is unspecified in that case and in practice, ...


4

NUL and / has their designated system functions. Other characters does not. That is the basics of it – the rest is opinions, speculations and history. Heard, read etc. and only included as a filler not a debate or argument: By forbidding certain characters you open up for complexity in the file system itself, which is the same as compromising it. What ...


4

You're seeing output to the terminal while it is not in NLCR mode (stty -onlcr). Normally the kernel driver for ttys outputs a carriage return along with a newline whenever a newline is output. With NLCR mode disabled newlines are output as is, which moves the cursor down one line but leaves it in the same column. This produces the staggered output you ...


4

I would be willing to bet that the problem is related to line endings. You're probably going through a non-*nix machine somewhere along the line. I also ran into a problem once where apache (running on Linux) was adding windows style line endings to uploaded text files so you might be seeing something similar. To test, take the file you downloaded and pass ...


3

You need to quote your expansion, otherwise it will undergo word splitting, which is what you are experiencing. acl=$(getfacl somefile.dat) echo "$acl" Bear in mind that $( strips trailing newlines anyway (it is considered a feature). If that is a problem for you, you need to do something like this to preserve them (in bash4+): mapfile acl < ...


3

sed is not able to eat newlines in a simple way. sed works on a line-by-line basis, so it can only add newlines, not remove them. But you can effectively do some sed gymnastics by storing two consecutive lines. You may also want to have a look at tr, which is able to replace characters, for example: tr '\n' , < file Also, with many tools you can use ...


3

Your shell script is in DOS/Windows text format (with CR+LF). Convert it to Unix format (e.g. use dos2unix). This won't work as you want though, as explained in the linked question. A new shell is executed, the variable it set, then the shell ends, taking the variable with it. If you have a script that uses the same shell as the one you're running (or has ...


3

With sed, you could do: sed 's/\\$//;/./,/^$/!d' One of the main problems with ed (beside its syntax from another age) is error handling in script. Basically, you have to think of it as ed returns success if it successfully edited the file. But to be a successful edit, you have to have each commands address to be valid, each command to be successful (for ...


3

A line without a trailing newline is not a line. some sed implementations will discard it, some will accept it among which some may add that missing newline back. In: echo -en 'abc\n' | sed -n 'N;p' sed starts with abc in its pattern space. Then proceeds with running the commands specified. The first one N is to append the next line to the pattern space. ...


3

If you are open to using bash, you could set IFS to new line and read from a here-string to split CAJA_SCRIPT_SELECTED_FILE_PATHS into an array. You can then access array elements individually or in a loop(using "${a[@]}"). Embedded spaces will be preserved. CAJA_SCRIPT_SELECTED_FILE_PATHS='a b c d f g' IFS=$'\n' read -d '' -r -a a <<< ...


3

You could define a shell function that pastes without the trailing newline. In zsh, the following command recalls the content of the clipboard, minus trailing newlines, and brings it up for editing (even if there are multiple lines): print -z `xsel -b` In bash, you can push the content of the clipboard minus trailing newlines to the history stack. After ...


3

Your scripts probably have DOS-style CR-LF line endings and not Unix-style LF line endings. The ^M seen in the error message in the first case is an indication that the 0D character was interpreted as part of the script interpreter name and not as part of the line ending (as one might expect it to be). Since there is no executable file on your system with a ...



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