【问题标题】:change eol character via cmd通过 cmd 更改 eol 字符
【发布时间】:2013-05-12 12:03:31
【问题描述】:

我有一个带有 unix eol 的文件 file1in, 我有一个脚本可以在其中进行一些编辑,但编辑是在output.txt 中完成的,并重命名为file1,这会将 eol 更改为 windows/dos

代码已给出

set uu=file1
set vv=file2

setlocal EnableDelayedExpansion
(for /F "delims=" %%a in (%uu%) do (
   set "line=%%a"
   if "!line:345=!" neq "!line!" (
      if "!line:123=!" neq "!line!" (
         if not defined flag (
            findstr "123" %vv% | findstr "345"
            set flag=true
         )
      ) else (
         echo !line!
      )
   ) else (
      echo !line!
   )
)) >output.txt 
del %uu% 
rename output.txt file1

有什么方法可以在没有用户输入的情况下通过 cmd 将其改回或保留 unix eol?

试过直接输入file1,得到一个0 kb的文件 试过type output.txt>file1给了dos/win eol 回显除空行以外的任何内容echo.>file1 更改了 eol 字符

【问题讨论】:

  • 你使用什么语言?
  • “unix”的 eol 是什么意思(例如 CR only、CRLFCR)?
  • 使用批处理脚本。 @endoro,真的不知道,直到现在我才使用 notepad++ 并将 EOL 覆盖从 windows/dos 更改为 unix/osx 格式。
  • 试过wiki,它说LF
  • 这是windows eol ` `

标签: cmd eol


【解决方案1】:

试试这个:

@echo off&setlocal enabledelayedexpansion
set "uu=file1"
set "vv=file2"
set LF=^


rem keep two empty lines between set LF and here
setlocal EnableDelayedExpansion
(for /F "delims=" %%a in (%uu%) do (
   set "line=%%a"
   if "!line:345=!" neq "!line!" (
      if "!line:123=!" neq "!line!" (
         if not defined flag (
            findstr "123" %vv% | findstr "345"
            set flag=true
         )
      ) else (
         <nul set/p"=!line!!LF!"
      )
   ) else (
      <nul set/p"=!line!!LF!"
   )
)) >output.txt 
type output.txt

【讨论】:

  • 不,仍然是 dos 格式。当我点击 show eol char 时,它显示 set lf=^ CRLF 而不是 LF。甚至尝试使用 unix 格式使其 LF 仍然有效
  • 不是所有的行都得到 only LF 最后,使用十六进制编辑器查看内部。请注意这两个空行,如果它们不是空的,这不起作用
【解决方案2】:

这里有一个解决方案:

@echo off
::Syntax: batchfile "file.txt" >"file2.txt"

:init
for /f %%c in ('copy /z "%~dpnx0" nul') do set cr=%%c
(set lf=^

)
del file.tmp 2>nul
for /f "delims=" %%i in ('findstr /n "^" "%~1"') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:*:=!!lf!"
echo(!line!
endlocal
)>>file.tmp
setlocal enabledelayedexpansion
findstr /v "!cr!!lf!" file.tmp
endlocal
del file.tmp 2>nul

【讨论】:

  • aaand 它无法运行,因为我有 64 位 :(
  • 我编辑了上面的帖子。我的原文如下:Horst Schaeffer 写了CRLF.COM,如果你有32 bit windows,它有一些聪明之处。 horstmuc.de/horst.htm
  • 你能解释一下它的工作原理吗?顺便说一句,我点击了显示符号,并且 lf 变量设置为 CRLF 而不是 LF
  • 是的,我也不知道在哪里替换文件名?对不起,如果我以菜鸟的身份出现
  • 如果你调用批处理文件 dos2unix.bat 那么命令将是dos2unix "filein.txt" &gt;"fileout.txt"
【解决方案3】:

使用纯 CMD 我会说不太可能。

GNU SED 应该能够进行这种转换。

sed -b s/\r//g infile >outfile

【讨论】:

  • gnu sed 不可移植,是吗?如果没有任何便携式解决方案?
  • 复制了同一行,但eol没有改变
  • GnuSED 做不到。 VBS 应该能够打开文件并使用 LF 写入行,否则没有行结尾。
  • 嗯 - 修改后的命令对我有用。 “便携”是什么意思?这是一个免费提供的实用程序。 GNU SED 是 DOS 风格。 Ux 有自己的版本。您针对哪些平台可能运行 CMD,而 GNU SED 可以在其上运行?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-20
  • 2012-07-23
  • 2019-08-05
  • 2019-11-18
  • 2014-06-23
  • 2020-07-19
  • 2021-12-25
相关资源
最近更新 更多