BAT文件来替换字符串中的两行 [英] BAT file to replace string in two lines
问题描述
我书面方式在Windows中,.bat文件的脚本,来代替:
请注意,有一个回车,我的意思是字符串在两行。现在看起来比较复杂:2个双引号之后有2个空格,然后按ENTER
2014年
那么,是这样的:(双引号)(双引号)(空间)(空间)(新行)(双引号)2014年
我有这个问题。这是脚本的样子:
关闭@echo
SETLOCAL enabledelayedexpansion
设置INTEXTFILE = FILE_IN_CSV.csv
设置OUTTEXTFILE = 222.csv
设置SEARCHTEXT = ^^^2014年
设置REPLACETEXT = ^^^4
设置OUTPUTLINE =FOR / F令牌= 1,* delims =¶%% A有('类型%INTEXTFILE%')做(
SET字符串= %%一
SET修改=字符串:%!SEARCHTEXT%=%REPLACETEXT%! 回音!修改! >> %OUTTEXTFILE%
)
德尔%INTEXTFILE%
重命名%OUTTEXTFILE%%INTEXTFILE%
任何想法?
谢谢!
编辑:添加源文本的例子。看到有3(而不是2!)黑色空间在每一行的末尾
2014091810000076;问题有关文件夹的大小;2014-09-18十二时10分20秒,2014年9月25日17时33分15秒, Resolt Sistemes;jbgsfnam;Sistemes :: Altres;STFD;peticions;
2014091910000109;错误打开Word文件与Office 2013,2014年9月19日9时50分07秒,2014年9月25日14时27分59秒,Resolt禁制令,yerttom; Qualitat :: Altres;STFD;peticions;
我只需要删除20在每行的开始,而不是其他20中的行中间
@ECHO OFF
SETLOCAL
(
FOR / Fdelims =%%一个IN(q6288377.txt)DO(
SET行= %%一
SETLOCAL ENABLEDELAYEDEXPANSION
!ECHO(^行:〜3!
ENDLOCAL
)
)> newfile.txt
(
FOR / Fdelims =%%一个IN(q6288377.txt)DO(
SET行= %%一
SETLOCAL ENABLEDELAYEDEXPANSION
IF!行:1,2〜!==20(ECHO(^行:!〜3)其他(回声(%%一)
ENDLOCAL
)
)> newfile2.txtGOTO:EOF
这是令人难以置信很难解决的表述为我的code没有做我想做的事情不理想的结果的明确说明了问题。
您的问题似乎是要删除的20在2列和文本文件的3(我的名字改为了适合我的系统。)
在code以上假设的您的文件开始的每一个行第一部分随意删除任何字符为2列3,创建
newfile.txt
,第二只选择那些具有行 20
2列3和删除这些列,只留下其他线路,并创建 newfile2.txt
。
您已经改变了从需求<大骨节病>空间骨节病> <大骨节病>空间骨节病> <大骨节病>换行骨节病>2014年3终端空间,第一行是无指示异常(它不会被,<大骨节病>空间骨节病> <大骨节病>空间骨节病> <大骨节病>换行骨节病> pceded $ p $)等上面可能不适用。无理念序列的presence是否显著与否 - 在所有的可能性,如 SED
A工具更适合这项任务(谷歌是你的朋友)
@ECHO OFF
SETLOCAL
SETsourcedir = U:\\ sourcedir
PUSHD%sourcedir%
FOR %%üIN(q6288377 * .TXT)DO(
(
FOR / Fusebackqdelims =%%一个IN(%% U)DO(
SET行= %%一
SETLOCAL ENABLEDELAYEDEXPANSION
!ECHO(^行:〜3!
ENDLOCAL
)
)&GT; %%〜nu.xtx
)
POPDGOTO:EOF
您将需要更改 sourcedir
的设置,以适应您的情况。生成的文件会的 samename 的.xtx。不要试图直接覆盖现有文件。
您还可以创建由prefixing在不同目录中的新文件 %%〜nu.xtx
提供一个目录名,如C:\\一些随机\\目录\\ %%〜nu.xtx - 用引号如果目录或文件名包含分隔符(如空格)。在〜NU
的意思是的名称 %% U
- 看到作为/ 的呢?提示获取更多信息。
它总是更容易解决,而不是零敲碎打的方式是整个问题。一个完整的描述可能会影响使用的方法。
I'm writting a script in Windows, a .BAT file, to replace:
Note that there is an ENTER, I mean the string is in two lines. And looks more complicated now: after the 2 double quotes there are 2 blank spaces and then the "ENTER"
""
"2014
So, something like: (double quote)(double quote)(space)(space)(newline)(double quote)2014
I'm having problems with this. This is how the script looks like:
@echo off
setlocal enabledelayedexpansion
set INTEXTFILE=FILE_IN_CSV.csv
set OUTTEXTFILE=222.csv
set SEARCHTEXT=^"^"^"2014
set REPLACETEXT=^"^"^"4
set OUTPUTLINE=
for /f "tokens=1,* delims=¶" %%A in ( '"type %INTEXTFILE%"') do (
SET string=%%A
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
echo !modified! >> %OUTTEXTFILE%
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%
Any ideas?
Thanks!
EDIT: adding source text example. See that there are 3 (not 2!) black spaces at the end of each line.
"2014091810000076";"Question about folder sizes";"2014-09-18 12:10:20";"2014-09-25 17:33:15";"Resolt Sistemes";"jbgsfnam";"Sistemes::Altres";"STFD";"peticions";""
"2014091910000109";"Error opening word files with office 2013";"2014-09-19 09:50:07";"2014-09-25 14:27:59";"Resolt TRO";"yerttom";"Qualitat::Altres";"STFD";"peticions";""
I just need to delete the 20 at the beginning of each line, but not the others 20 in the middle of lines.
@ECHO OFF
SETLOCAL
(
FOR /f "delims=" %%a IN (q6288377.txt) DO (
SET line=%%a
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO(^"!line:~3!
endlocal
)
)>newfile.txt
(
FOR /f "delims=" %%a IN (q6288377.txt) DO (
SET line=%%a
SETLOCAL ENABLEDELAYEDEXPANSION
IF "!line:~1,2!"=="20" (ECHO(^"!line:~3!) else (echo(%%a)
endlocal
)
)>newfile2.txt
GOTO :EOF
It's incredibly difficult to solve a problem stated as "my code doesn't do what I want it to do" without an clear indication of desired outcome.
Your problem appears to be to delete the "20" in columns 2 and 3 of the text file (I changed the name to suit my system.)
The code above assumes that every line of your file starts "
. The first part arbitrarily deletes whatever characters are in columns 2 and 3, creating newfile.txt
. The second selects only those lines which have 20
in columns 2 and 3 and deletes those columns, leaving other lines alone and creates newfile2.txt
.
You have changed the requirement from ""SpaceSpaceNewline"2014 to 3 terminal spaces, No indication of whether the first line is an exception (it wouldn't be preceded by ""SpaceSpaceNewline) and so the above may not be applicable. No idea whether the presence of the sequences is significant or not - and in all probability, a utility like sed
is more suited to the task (Google is your friend)
[edit for extended question]
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
PUSHD "%sourcedir%"
FOR %%u IN (q6288377*.txt) DO (
(
FOR /f "usebackqdelims=" %%a IN ("%%u") DO (
SET line=%%a
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO(^"!line:~3!
endlocal
)
)>%%~nu.xtx
)
popd
GOTO :EOF
You would need to change the setting of sourcedir
to suit your circumstances. File produced would be samename.xtx. Don't try to overwrite the existing files directly.
You could also create new files in a different directory by prefixing %%~nu.xtx
with a directory name, eg "c:\some random\directory\%%~nu.xtx" - "enclose in quotes" if the directory or filenames contain separators (like spaces). The ~nu
means "the name of %%u
- see for /? from the prompt for more information.
It's always easier to solve an entire problem than to approach it piecemeal. A full description may influence the method used.
这篇关于BAT文件来替换字符串中的两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!