Questions tagged [newlines]

Use for questions about representation and manipulation of the end-of-line marker in text files, in particular when editing the same file on different operating systems where the representation may vary.

Text files are files in which data is represented in a line-based format as human-readable characters.

Apart from the general issue of , a special challenge is that even within the same character encoding framework, the representation of the "end-of-line"-indicator is not standardized across all operating systems.

  • DOS and Windows use a CR+LF sequence to indicate the end of the current line
  • UNIX, Linux and related operating systems use a single LF character to indicate the end of the current line.
  • Other conventions are also known to exist, although they are not widespread any more.

This can lead to problems when editing a text file on an operating system using one convention after the same file was edited on a system using another convention.

This tag should be used on questions that deal with the problems arising from such situations, and the text and file processing tools used to overcome the problem or ensure compatibility of a file with the current OS's end-of-line style.

312 questions
314
votes
7 answers

What's the point in adding a new line to the end of a file?

Some compilers (especially C or C++ ones) give you warnings about: No new line at end of file I thought this would be a C-programmers-only problem, but github displays a message in the commit view: \ No newline at end of file for a PHP file. I…
Philipp Stephan
  • 3,250
  • 2
  • 15
  • 8
277
votes
19 answers

How to add a newline to the end of a file?

Using version control systems I get annoyed at the noise when the diff says No newline at end of file. So I was wondering: How to add a newline at the end of a file to get rid of those messages?
k0pernikus
  • 14,853
  • 21
  • 58
  • 79
176
votes
3 answers

Why ZSH ends a line with a highlighted percent symbol?

I've noticed this on occasion with a variety of applications. I've often thought it was because the output was cancelled early (ctrl+c, for example) or something similar, and zsh is filling in a new line character. But now curiosity has gotten the…
jktravis
  • 2,156
  • 2
  • 13
  • 15
106
votes
5 answers

How do I remove the newline from the last line in a file in order to add text to that line?

Suppose I have a file called file: $ cat file Hello Welcome to Unix I want to add and Linux at the end of the last line of the file. If I do echo " and Linux" >> file will be added to a new line. But I want last line as Unix and Linux So, in order…
Pandya
  • 23,898
  • 29
  • 92
  • 144
98
votes
11 answers

How to test whether a file uses CRLF or LF without modifying it?

I need to periodically run a command that ensures that some text files are kept in Linux mode. Unfortunately dos2unix always modifies the file, which would mess file's and folder's timestamps and cause unnecessary writes. The script I write is in…
Adam Ryczkowski
  • 5,493
  • 7
  • 39
  • 60
93
votes
4 answers

Why does Linux use LF as the newline character?

As far as I know, every operating system has a different way to mark the end of line (EOL) character. Commercial operating systems use carriage return for EOL (carriage return and line feed on Windows, carriage return only on Mac). Linux, on the…
Bagas Sanjaya
  • 1,092
  • 1
  • 7
  • 9
86
votes
5 answers

How to put a newline special character into a file using the echo command and redirection operator?

I would like to create a file by using the echo command and the redirection operator, the file should be made of a few lines. I tried to include a newline by "\n" inside the string: echo "first line\nsecond line\nthirdline\n" > foo but this way no…
Abdul Al Hazred
  • 25,760
  • 23
  • 64
  • 88
64
votes
5 answers

Why do newline characters get lost when using command substitution?

I have a text file named links.txt which looks like this link1 link2 link3 I want to loop through this file line by line and perform an operation on every line. I know I can do this using while loop but since I am learning, I thought to use a for…
user3138373
  • 2,441
  • 6
  • 29
  • 44
57
votes
10 answers

Finding all "Non-Binary" files

Is it possible to use the find command to find all the "non-binary" files in a directory? Here's the problem I'm trying to solve. I've received an archive of files from a windows user. This archive contains source code and image files. Our build…
Alana Storm
  • 1,413
  • 3
  • 14
  • 17
51
votes
3 answers

Removing all spaces, tabs, newlines, etc from a variable?

This is the error I am getting and it's failing because of a variable whose value is supposed to be 2 (I am getting this using a select * from tabel). I am getting spaces in that variable. + 0 != 2 ./setjobs[19]: 0: not found. How do I…
munish
  • 7,825
  • 24
  • 71
  • 97
42
votes
6 answers

How to fix "Hunk #1 FAILED at 1 (different line endings)" message?

I am trying to create a patch with the command git diff sourcefile >/var/lib/laymab/overlay/category/ebuild/files/thepatch.patch when I apply the patch, it gives me $ patch -v GNU patch 2.7.5 $ /usr/bin/patch -p1…
user1709408
  • 669
  • 1
  • 9
  • 14
40
votes
8 answers

How to add a carriage return before every newline?

I have a file that only uses \n for new lines, but I need it to have \r\n for each new line. How can I do this? For example, I solved it in Vim using :%s/\n/\r\n/g, but I would like to use a script or command-line application. Any suggestions? I…
modulitos
  • 3,097
  • 8
  • 29
  • 44
40
votes
5 answers

Replace \n by a newline in sed portably

I'm so confused with GNU sed, POSIX sed, and BSD sed's myriad of options. How do I replace literal \n with the newline character using these three sed types?
Avinash Raj
  • 3,653
  • 4
  • 20
  • 34
39
votes
4 answers

How to detect end of line with sed

I'm looking for a way to only execute replacement when the last character is a newline, using sed. For instance: lettersAtEndOfLine is replaced, but this is not: lettersWithCharacterAfter& Since sed does not work well with newlines, it is not as…
Matthew D. Scholefield
  • 1,009
  • 1
  • 10
  • 17
36
votes
6 answers

Why is the end-of-line $ anchor not working with the grep command, even though the front-of-line ^ anchor is?

Very new to UNIX but not new to programming. Using Terminal on MacBook. For the purposes of managing and searching word lists for crossword construction, I'm trying to get handy with the Grep command and its variations. Seems pretty straightforward…
DTalvacchio
  • 361
  • 1
  • 3
  • 4
1
2 3
20 21