C# - 使用 Tectia 将文件从 Windows 传输到 unix 时如何删除 ctrl M 字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12508032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:29:25  来源:igfitidea点击:

C# - how to remove ctrl M characters when transferring files from windows to unix using Tectia?

c#windowsunixnewline

提问by Ravi Goklani

In a C# module, I want to transfer files from Windows to Unix using Tectia. But the problem is when these files are transferred(Ascii or Binary mode both) and opened using VI editor we get ^M characters. I searched about this but the solutions are to remove these ^M characters after the files are transferred using utilites. Is there any way that these ^M characters do not appear in the first place. Is there any option to have a workaround in code before sending these files?

在 C# 模块中,我想使用 Tectia 将文件从 Windows 传输到 Unix。但问题是当这些文件被传输(Ascii 或二进制模式)并使用 VI 编辑器打开时,我们得到 ^M 字符。我对此进行了搜索,但解决方案是在使用实用程序传输文件后删除这些 ^M 字符。有什么办法可以使这些 ^M 字符首先不出现。在发送这些文件之前,是否有任何选项可以在代码中解决方法?

采纳答案by Ravi Goklani

Thanks for all the help everyone. I solved this problem using a workaround. Windows uses CR+LF (\r\n) as end-of-line & Unix uses LF (\n) as end-of-line.I took the Windows file and replaced the occurrence of CR+LF(\r\n) with LF(\n) in code itself without any utility.This made the file compatible for Unix systems and then I transferred the file using SFTP and it worked on Unix without ^M characters.

谢谢各位的帮助。我使用一种解决方法解决了这个问题。Windows 使用 CR+LF (\r\n) 作为行尾 & Unix 使用 LF (\n) 作为行尾。我拿了 Windows 文件并替换了 CR+LF(\r\n ) 与 LF(\n) 代码本身没有任何实用程序。这使文件与 Unix 系统兼容,然后我使用 SFTP 传输文件,它在没有 ^M 字符的 Unix 上工作。

回答by alfasin

You can install and use dos2unix. After Installation just run:

您可以安装和使用dos2unix。安装后只需运行:

>dos2unix yourfile.txt 

回答by Quetzalcoatl

Try executing the following in your terminal (may need to install it first):

尝试在终端中执行以下操作(可能需要先安装):

fromdos <your-file>

回答by Vinoth

One more trick to remove Ctrl+Min vi editor:

在 vi 编辑器中删除Ctrl+ 的另一个技巧M

:%s/^V^M//g

For more trick to remove Ctrl+Mcharacters

有关删除+字符的更多技巧CtrlM

回答by Lakshminarayanan

:%s/^V^M//
  1. gnot needed at all.

  2. %s- find and replace text that u want to replace

  3. ^V^Mto be replaced with "should be after double slash" keep it empty if you want to replace with nothing

  1. g根本不需要。

  2. %s- 查找并替换您要替换的文本

  3. ^V^M被替换为“应该在双斜线之后”如果你想什么都不替换,请将其留空

回答by Rajesh

How I was able to remove it in vi editor:

我是如何在 vi 编辑器中删除它的:

  • After :%s/then press ctrl+Vthen ctrl+M. This will give you ^M
  • Then //g(will look like: :%s/^M) press Entershould get all removed.
  • 之后:%s/再按下 ctrl+Vctrl+ M。这会给你^M
  • 然后//g(看起来像:%s/^M:)按下Enter应该被全部删除。

Good luck!

祝你好运!

回答by Akshay

Try

尝试

tr -d '5' <INPUT_FILE > OUTPUT_FILE

回答by JellicleCat

If you just need to remove the ^Mcharacters (not replace them with \n):

如果您只需要删除^M字符(而不是用 替换它们\n):

sed -i -e 's/\r//g' yourfile.txt

If you want to replace them with \n:

如果您想将它们替换为\n

sed -i -e 's/\r/\n/g' yourfile.txt

回答by Niket

tr '\015' '\n' < /tmp/file-with-ctrlm.txt > /tmp/no-ctrlm.txt

tr '\015' '\n' < /tmp/file-with-ctrlm.txt > /tmp/no-ctrlm.txt

  • This command uses ASCII code of CtrlM and replaces all CtrlM with newline characters.
  • It avoids Ctrl-V, Ctrl-M, and works on Mac too.
  • 此命令使用 CtrlM 的 ASCII 代码并用换行符替换所有 CtrlM。
  • 它避免了 Ctrl-V、Ctrl-M,并且也适用于 Mac。

回答by RmccurdyDOTcom

For me this is the only thing that would work .. not unix2dos or anything ...

对我来说,这是唯一可行的方法......不是 unix2dos 或任何东西......

sed -e 's/\x0D//g'

sed -e 's/\x0D//g'

reference:

参考:

The hexadecimal 0a, a control character as opposed to a printing character, is called a line feed.

十六进制 0a 是与打印字符相对的控制字符,称为换行符。

The hexadecimal 0d is called a carriage return.

十六进制的 0d 称为回车。