使用批处理脚本读取文本文件并以不同的顺序将特定文本复制到新的文本文件中[关闭]
Posted
技术标签:
【中文标题】使用批处理脚本读取文本文件并以不同的顺序将特定文本复制到新的文本文件中[关闭]【英文标题】:Read text file and copy specific text in different order into new text file using a batch script [closed] 【发布时间】:2021-04-25 13:47:14 【问题描述】:希望针对一个非常具体的问题寻求帮助。我是批处理编程的新手,想使用批处理文件归档以下内容。
读取 textfile1 的所有行并将值以不同的顺序放入带有标题行的 textfile2 中。
示例:textfile1(输入文件) 我想从这个文件中逐行读取并选择某些内容。每行的结构是一种 label="Value"spacelabel"Value"space 等等(试图在下面演示):
Supplier CountryName="GB" SupplierNumber="1112|DISCOUNT|497" Street="ANDERTON HOUSE" CountryCoded="GB" Name1="ANDERTON BOARD AND PACKAGING" CorporateGroupID="497"
Supplier CountryName="GB" SupplierNumber="113093|AMB HEAD OFFICE|846" Street="Langcliffe Paper Mills" CountryCoded="GB" Name1="JOHN ROBERTS HOLDINGS LTD" CorporateGroupID="846"
示例:textfile2(输出文件) 第一行应该是每个标签由 TAB 分隔的标题行,在下面我想为每个字段写入在 textfile1 中找到的值。 如果某个字段没有值,那么我想在添加新找到的值之前添加一个 TAB。 如您所见,我只想选择引号之间的值,而不是别的。 textfile1 的结构始终相同(顺序不变),标签也相同。每行总是以 Supplier CountryName= 开头 只是想知道这是否可以使用批处理文件。
SupplierNumber Location CorporateGroupID Name1 Name2 Description POBox CountryCoded
1112|DISCOUNT|497 497 ANDERTON BOARD AND PACKAGING GB
113093|AMB HEAD OFFICE|846 846 JOHN ROBERTS HOLDINGS LTD GB
对此的任何意见都会很有帮助,谢谢。
【问题讨论】:
【参考方案1】:@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "filename1=%sourcedir%\q65822358.txt"
SET "outfile=%destdir%\outfile.txt"
:: Special characters
:: the variable TAB should be set to a tab character. This may not be copy/pastable from SO code to editors.
:: Make sure that this is a single TAB as intended, not a space-sequence
SET "tab= "
SET "columnsrequired=SupplierNumber Location CorporateGroupID Name1 Name2 Description POBox CountryCoded"
SET "header="
FOR %%c IN (%columnsrequired%) DO SET "%%c="&SET "header=!header!%tab%%%c"
(
ECHO %header:~1%
FOR /f "usebackqdelims=" %%b IN ("%filename1%") DO (
FOR %%c IN (%columnsrequired%) DO SET "%%c="
SET "lineread=%%b"
FOR %%c IN (%columnsrequired%) DO (SET "%%c=!lineread:*%%c=!")
rem FOR %%c IN (%columnsrequired%) DO ECHO %%c is '!%%c!'
rem each variable now contains either the full original line or '="required value" waffle...`
FOR %%c IN (%columnsrequired%) DO (
SET "lineread=!%%c!"
IF "!lineread:~0,1!"=="=" (
rem change " to \ as " is a special character in batch
SET "lineread=!lineread:"=\!"
rem ECHO lineread "!lineread!"
FOR /f "tokens=2delims=\" %%x IN ("!lineread!") DO SET "%%c=%%x"
) ELSE (SET "%%c=")
)
SET "header="
FOR %%c IN (%columnsrequired%) DO SET "header=!header!%tab%!%%c!"
ECHO !header:~1!
)
)>"%outfile%"
GOTO :EOF
【讨论】:
你是一个阵营!非常感谢您的回复。这正是我所追求的。你完全让我开心,干杯。【参考方案2】:你的“问题”只是一个代码请求,通常在这个论坛上被删除......但是,我有一些空闲时间,所以这里是你要求的解决方案:
@echo off
setlocal EnableDelayedExpansion
rem Define output fields
set "fields=SupplierNumber Location CorporateGroupID Name1 Name2 Description POBox CountryCoded"
rem Get TAB character
for /F "delims=pR tokens=1,2" %%a in ('reg query hkcu\environment /v temp' ) do set "TAB=%%b"
rem Assemble Header and Output "format" lines, and initialize fields
set "header="
set "output="
for %%a in (%fields%) do (
set "header=!header!%%a!TAB!"
set "output=!output!^!%%a:~1,-1^!!TAB!"
set "%%a="
)
rem Process the file
(
echo %header:~0,-1%
for /F "tokens=1*" %%a in (textfile1.txt) do (
call :GetFields %%b
echo %output:~0,-1%
)) > textfile2.txt
goto :EOF
:GetFields
set line=%*
set %line:" ="&set %
exit /B
textfile1.txt:
Supplier CountryName="GB" SupplierNumber="1112|DISCOUNT|497" Street="ANDERTON HOUSE" CountryCoded="GB" Name1="ANDERTON BOARD AND PACKAGING" CorporateGroupID="497"
Supplier CountryName="GB" SupplierNumber="113093|AMB HEAD OFFICE|846" Street="Langcliffe Paper Mills" CountryCoded="GB" Name1="JOHN ROBERTS HOLDINGS LTD" CorporateGroupID="846"
textfile2.txt:
SupplierNumber Location CorporateGroupID Name1 Name2 Description POBox CountryCoded
1112|DISCOUNT|497 497 ANDERTON BOARD AND PACKAGING GB
113093|AMB HEAD OFFICE|846 846 JOHN ROBERTS HOLDINGS LTD GB
【讨论】:
太棒了,感谢您对我的“问题”的回答。非常感谢你能帮助我。非常感谢您花一些时间在这上面。干杯。以上是关于使用批处理脚本读取文本文件并以不同的顺序将特定文本复制到新的文本文件中[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
Python函数:使用批处理文件将参数从.txt文件传递给python函数并执行函数