- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个文件 (1.8 Mb),其中有 1 行(很长的)文本。该行上的值通常由 13 个空格分隔。我想做的是用管道 | 替换这 13 个空格分隔符,以便我可以使用 SSIS 处理此文本文件。
到目前为止,我没有成功地使用批处理文件以编程方式处理此文件。
我尝试使用从另一篇 SO 帖子中获得的以下代码。
@echo off
REM create empty file:
break>R1.txt
setlocal enabledelayedexpansion
REM prevent empty lines by adding line numbers (find /v /n "")
REM parse the file, taking the second token (*, %%b) with delimiters
REM ] (to eliminate line numbers) and space (to eliminate leading spaces)
for /f "tokens=1,* delims=] " %%a in ('find /v /n "" ^<PXZP_SND_XZ01_GFT10553.dat') do (
call :sub1 "%%b"
REM write the string without quotes:
REM removing the qoutes from the string would make the special chars poisonous again
>>PXZP_SND_XZ01_GFT10553.dat echo(!s:"=!
)
REM Show the written file:
type PXZP_SND_XZ01_GFT10553.dat
goto :eof
:sub1
set S=%*
REM do 13 times (adapt to your Needs):
for /l %%i in (1,1,13) do (
REM replace "space qoute" with "quote" (= removing the last space
set S=!S: "=|!
)
goto :eof
有人可以帮我吗?我的文本文件示例:
96859471/971 AAAA HAWAII 96860471/971 BBBB HAWAII 96861471/971 CCCC HAWAII 96863471/971 DDDD HAWAII
最佳答案
for /F
loop无法处理超过 8190 个字符的行。但是,有一种方法可以读取行数较长的文件:使用 set /P
在一个循环中,连同 input redirection <
; set /P
最多读取 1023 个字符,除非遇到换行符或文件末尾;自 set /P
以来,对同一个打开(输入重定向)文件句柄多次执行它允许读取 1023 个字符部分的非常长的行不重置文件指针。
另一个挑战是返回(回显)很长的行,这对于 echo
command 是不可能的再次因为大约 8190 个字符的行限制(适用于命令行和变量内容)。同样,按 block 处理也有帮助:首先,获取文件结束字符(EOF,ASCII 0x1A);然后取一个文本/字符串部分,附加一个 EOF 并使用 echo
将结果写入临时文件(附加一个换行符),连同 output redirection >
;接下来使用 copy
将文件复制到自身上, 但以 ASCII 文本模式读取它以丢弃 EOF 和之后的所有内容(因此先前由 echo
附加的换行符)并以二进制模式写入它以获得结果数据的精确副本;最后,使用 type
输入文件内容.
以下脚本使用了这些技术(请参阅代码中的所有解释性 rem
注释):
@echo off
setlocal EnableExtensions DisableDelayedexpansion
rem // Define constants here:
set "_INPUT=.\PXZP_SND_XZ01_GFT10553.dat" & rem // (this is the input file)
set "_OUTPUT=.\R1.txt" & rem // (set to `con` to display the result on the console)
set "_TEMPF=%TEMP%\%~n0_%RANDOM%.tmp" & rem // (specifies a temporary file)
set "_SEARCH= " & rem // (this is the string to be found)
set "_REPLAC=|" & rem // (this is the replacement string)
set "_LTRIM=#" & rem // (set to something to left-trim sub-strings)
(set _LF=^
%= blank line =%
) & rem // (this block stores a new-line character in a variable)
rem // This stores an end-of-file character in a variable:
for /F %%E in ('forfiles /P "%~dp0." /M "%~nx0" /C "cmd /C echo 0x1A"') do set "_EOF=%%E"
rem /* The input file is going to be processed in a sub-routine,
rem which accesses the file content via input redirection `<`: */
< "%_INPUT%" > "%_OUTPUT%" call :PROCESS
endlocal
exit /B
:PROCESS
rem // Reset variables that store a partial string to be processed and a separator:
set "PART=" & set "SEP="
setlocal EnableDelayedExpansion
:READ
rem /* At this point 1023 characters are read from the input file at most, until
rem a line-break or the end of the file is encountered:*/
set "NEW=" & set /P NEW=""
rem // The read characters are appended to a string buffer that will be processed:
set "PART=!PART!!NEW!"
rem /* Skip processing when the string buffer is empty, which is the case when the end
rem of the file has already been reached: */
:LOOP
if defined PART (
rem /* Make the search string accessible as a `for` meta-variable reference in
rem to not have to use normal (immediate) `%`-expansion, which could cause
rem trouble with some special characters under some circumstances: */
for /F delims^=^ eol^= %%K in ("!_SEARCH!") do (
rem /* Try to split the string buffer at the first search string and store the
rem portion at the right, using sub-string substitution: */
set "RIGHT=!PART:*%%K=!"
rem /* Check whether the split was successful, hence whether a search string
rem even occurred in the string buffer; if not, jump back and read more
rem characters; otherwise (when the end of the file was reached) clear the
rem right portion and continue processing: */
if "!RIGHT!"=="!PART!" if not defined NEW (set "RIGHT=") else goto :READ
rem /* Clear the variable that will receive the portion left to the first
rem occurrence of the search string in the string buffer; then replace each
rem occurrence in the string buffer by a new-line character: */
set "LEFT=" & set ^"PART=!PART:%%K=^%_LF%%_LF%!^"
rem /* Iterate over all lines of the altered string buffer, which is now a
rem multi-line string, then get the first line, which constitutes the
rem portion at the left of the first search string; the (first) line is
rem preceded by an `_` just for it not to appear blank, because `for /F`
rem skips over empty lines; this character is removed later: */
for /F delims^=^ eol^= %%L in (^"_!PART!^") do (
rem // Execute the loop body only for the first iteration:
if not defined LEFT (
rem /* Store the (augmented) left portion with delayed expansion
rem disabled in order not to get trouble with `!` in the string: */
setlocal DisableDelayedExpansion & set "LEFT=%%L"
rem // Enable delayed expansion to be able to safely echo the string:
setlocal EnableDelayedExpansion
rem /* Write to a temporary file the output string, which consists of
rem a replacement string (except for the very first time), the left
rem portion with the preceding `_` removed and an end-of-file
rem character; a line-break is automatically appended by `echo`: */
> "!_TEMPF!" echo(!SEP!!LEFT:~1!%_EOF%
rem /* Copy the temporary file onto itself, but remove the end-of-file
rem character and everything after, then type the file content;
rem this is a safe way of echoing a string without a line-break: */
> nul copy /Y /A "!_TEMPF!" + nul "!_TEMPF!" /B & type "!_TEMPF!"
rem /* Restore the environment present at the beginning of the loop
rem body, then ensure the left portion not to appear empty: */
endlocal & endlocal & set "LEFT=_"
)
)
rem // If specified, left-trim the right portion, so remove leading spaces:
if defined _LTRIM (
for /F "tokens=* eol= delims= " %%T in ("!RIGHT!_") do (
for /F delims^=^ eol^= %%S in (^""!NEW!"^") do (
endlocal & set "NEW=%%~S" & set "RIGHT=%%T"
)
setlocal EnableDelayedExpansion & set "RIGHT=!RIGHT:~,-1!"
)
)
rem // Set the replacement string now to skip it only for the first output:
set "SEP=!_REPLAC!"
rem /* Move the right portion into the string buffer; if there is still some
rem amount of text left, jump back to find more occurrences of the search
rem string; if not, jump back and read more characters, unless the end of
rem the file has already been reached: */
set "PART=!RIGHT!" & if defined PART (
if defined NEW if "!PART:~1024!"=="" goto :READ
goto :LOOP
) else if defined NEW goto :READ
)
)
endlocal
rem // Clean up the temporary file:
del "%_TEMPF%"
exit /B
存在以下限制:
!
开头, *
, ~
并且不得包含 =
;!
;关于batch-file - 需要从 1 行很长的文本文件中替换 13 个空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55503031/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!