Edit file in-place without modifying line endings

0

0

I need to modify sources under Git version control. The sources are Visual Studio project files, which makes them native Microsoft. I want to avoid whitespace issues, including line ending games.

I looked at the files before the modification under emacs, and the ^M was clearly not present.

I then executed the following from a script from a Linux machine:

sed -i 's/odbc32.lib //g' *.vcproj
sed -i 's/odbccp32.lib //g' *.vcproj

When I look at the changes:

$ git diff cryptest.vcproj
diff --git a/cryptest.vcproj b/cryptest.vcproj
index cec447d..9ae71ea 100644
--- a/cryptest.vcproj
+++ b/cryptest.vcproj
@@ -76,7 +76,7 @@
...
Name="VCLinkerTool"
-        AdditionalDependencies="odbc32.lib odbccp32.lib Ws2_32.lib"
+        AdditionalDependencies="Ws2_32.lib"^M
        SuppressStartupBanner="true"
...

How do I perform a replacement without adding extraneous whitespace?

jww

Posted 2015-07-17T23:04:13.700

Reputation: 1

Answers

1

These days emacs will automatically detect the line endings used in a file and so you will not see whether a file has \n or \r or \r\n line endings. Any edits in emacs will preserve and imitate the line endings.

To really see what the file has you can try cat -vet which shows control chars including ^M, tabs (^I), and the end of line ($).

Note that git can be configured to automatically convert \r\n to \n or vice versa when moving text files between the working directory and the object database. See this article and this article to check how your git is configured, globally and per-repository and how you can change it.

It's difficult to see how sed could have introduced the \r. I'm assuming you are on a Unix system.


At the top of your working directory check that the following commands do not show anything:

git config core.eol
git config core.autocrlf
find . -name .gitattributes -o -name attributes

Run the original unedited file through cat -vet and check whether all, some, or none of the lines end with the 3 characters ^M$.

meuh

Posted 2015-07-17T23:04:13.700

Reputation: 4 273

*"I'm assuming you are on a Unix system..."* - yes (kind of). I'm on a Linux system, not Unix: "...I then executed the following from a script from a Linux machine"" . – jww – 2015-07-19T03:00:10.680

To get back to the orginal question: "How do I perform a replacement without adding extraneous whitespace?". I don't think that's provided in your answer. – jww – 2015-07-19T03:02:06.173

At the top of your working directory can you try the following commands: git config core.eol git config core.autocrlf find . -name .gitattributes -o -name attributes and if they show anything add the results to your question. Also, does cat -vet cryptest.vcproj before the edit show the line endings as 3 chars ^M$ on all lines, only some, or none. – meuh – 2015-07-19T07:11:04.963