batch-file - 使用批处理脚本读取文本文件并以不同的顺序将特定文本复制到新的文本文件中
问题描述
希望帮助解决一个非常具体的问题。我是批处理编程的新手,想使用批处理文件存档以下内容。
读取 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
对此的任何意见都会非常有帮助,谢谢。
解决方案
@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
推荐阅读
- android - Android dp 到 px 舍入
- javascript - 映射并返回 _.uniq 值而不重复
- ckan - Ckan、Redis 和重复数据集
- r - 叠加密度图,ggplot2,R 3.5.1。,错误:美学必须是长度1或与数据相同
- php - 如何设置环境而不是 .env 文件?
- node.js - 使用 vue-nodejs 映射应用程序
- apache - 我可以分解我的 VirtualHost 配置吗?
- git - 为 GitLab CI 禁用 GIT LFS
- keystore - 以太坊密钥库文件加密或损坏?
- javascript - 在您的项目中找不到 Typescript 编译器,对 typescript 文件的分析被中止