Skip to content

Instantly share code, notes, and snippets.

@qqwqqw689
Last active May 23, 2024 08:39
Show Gist options
  • Save qqwqqw689/487505cdad3120f030658c614845f527 to your computer and use it in GitHub Desktop.
Save qqwqqw689/487505cdad3120f030658c614845f527 to your computer and use it in GitHub Desktop.
Converting between Windows and Linux line breaks.

The Windows and Linux operating systems have a slight variation in how they handle newlines in files. Typically, in the Windows environment a line is terminated with the two characters \r\n. The \r character represents a carriage return, and \n represents a newline. In Linux, only the \n character is used to terminate a line.

This causes some interesting behavior issues when moving between the Windows and Linux environment. For example, if you have a multi-line text file that was created in Linux, and then try to open it using a program such as Windows Notepad, the entire contents of the file will appear on a single line.

Converting from Linux to Windows Line Breaks

You can use the sed command to convert the file fileLinux.txt to Windows line breaks:

sed -i 's/$/\r/' fileLinux.txt

The -i option tells sed to write the results back to the input file. The s is sed's substitute command. The $ is a regular expression that matches the end of a line, and \r is the carriage return.

Converting from Linux to Windows Line Breaks

To convert from Windows to Linux line breaks you can use the tr command and simply remove the \r characters from the file.

sed -i 's/\r//' fileWindows.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment