gpt4 book ai didi

batch-file - 我如何从批处理文件中的多行中获取此文本

转载 作者:行者123 更新时间:2023-12-05 06:57:45 24 4
gpt4 key购买 nike

我有一个文本文件 (Myfile.txt),逐行显示,它很长且居中,如下所示

...","ItemPrice":17000.0,"MustPay":17000.0,"PaywithCash":17000.0,"etc...
...","ItemPrice":900.0,"MustPay":900.0,"PaywithCash":900.0,"etc...
...","ItemPrice":1400.0,"MustPay":1400.0,"PaywithCash":1400.0,"etc...

所以我想得到“PayWithCash”这个词后面的数字:例如第一行是数字17000等等到下一行,并将其保存到一个新的文本文件“result.txt”

1700
900
1400

我试过几种代码,包括如下

echo off 
SETLOCAL EnableDelayedExpansion
for /f "delims=" %%a in ('type Myfile.txt^|find "PayWithCash:"') do (
set "line=%%a"
set "line=!line:*PayWithCash =!
set /a "last=!line:~1!" 2>nul
)
echo %last% >> result.txt

是的,我还是得不到想要的结果,你能帮帮我吗?

我用的是谷歌翻译,希望你能理解

最佳答案

PayWithCash 改正为数据中的正确大小写,PaywithCash 或将 /i 开关添加到 find使匹配不区分大小写。

按照@Compo 的建议删除冒号。

删除set语句中PayWithCash后的Space,因为数据中没有这样的Space

因为 line 中的结果现在开始 ": 你需要使用 line:~2

我使用的测试代码:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q64838297.txt"
SET "outfile=%destdir%\outfile.txt"

(
for /f "delims=" %%a in ('type "%filename1%"^|find /i "PayWithCash"') do (
set "line=%%a"
set "line=!line:*PayWithCash=!"
set /a "last=!line:~2!" 2>nul

echo !last!
)
)>"%outfile%"

TYPE "%outfile%"

GOTO :EOF

您需要更改 sourcedirdestdir 的设置以适合您的情况。该列表使用适合我的系统的设置。

我使用了一个名为 q64838297.txt 的文件,其中包含您的数据用于我的测试。

生成定义为 %outfile%

的文件

结果:

17000
900
1400

关于batch-file - 我如何从批处理文件中的多行中获取此文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64838297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com