我想从txt文件中查找特定字符

发布于 2025-01-09 03:10:33 字数 863 浏览 0 评论 0原文

早上好,我想从txt文件中查找特定字符并创建一个新的txt文件。该文件包含以下行:

Server: http://esv5.net:80/c/ 
MAC: 00:1a:79:17:52:ae 
Vence:February 28, 2022, 11:35 pm  
http://mag.abcdxyz.one/c
00:1a:79:d6:3f:2a;January 4, 2023, 3:17 pm 
00:1a:79:2e:e7:e1;July 30, 2022, 12:18 pm 
....
....

我希望以相同的顺序输出:

http://esv5.net:80/c/ 
00:1a:79:17:52:ae 
http://mag.abcdxyz.one/c
00:1a:79:d6:3f:2a
00:1a:79:2e:e7:e1

我的批处理代码是:

@echo off 
FIND "http://" "C:\Users\dom19\Downloads\file.txt"
FIND "00:1a:79" "C:\Users\dom19\Downloads\file.txt"
TYPE C:\Users\dom19\Downloads\file.txt > C:\Users\dom19\Downloads\Newfile.txt
pause

我添加此命令来保存结果而不是保存: <代码>> C:\Users\dom19\Desktop\results.txt 它就是这样工作的

C:\Users\dom19\Desktop\script.bat >> "C:\Users\dom19\Desktop\file.txt"

Good morning, I want to find specific characters from txt file and create a new txt file. The file contain this lines:

Server: http://esv5.net:80/c/ 
MAC: 00:1a:79:17:52:ae 
Vence:February 28, 2022, 11:35 pm  
http://mag.abcdxyz.one/c
00:1a:79:d6:3f:2a;January 4, 2023, 3:17 pm 
00:1a:79:2e:e7:e1;July 30, 2022, 12:18 pm 
....
....

I want this output in same order:

http://esv5.net:80/c/ 
00:1a:79:17:52:ae 
http://mag.abcdxyz.one/c
00:1a:79:d6:3f:2a
00:1a:79:2e:e7:e1

My batch code is:

@echo off 
FIND "http://" "C:\Users\dom19\Downloads\file.txt"
FIND "00:1a:79" "C:\Users\dom19\Downloads\file.txt"
TYPE C:\Users\dom19\Downloads\file.txt > C:\Users\dom19\Downloads\Newfile.txt
pause

I add thiss command to save results bat not save:
> C:\Users\dom19\Desktop\results.txt
it just works like that

C:\Users\dom19\Desktop\script.bat >> "C:\Users\dom19\Desktop\file.txt"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉月流沐 2025-01-16 03:10:33
@ECHO OFF
SETLOCAL
rem The following settings for the source directory & filenames 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 "filename1=%sourcedir%\q71207091.txt"

FOR /f "usebackqtokens=1*delims=: " %%b IN ("%filename1%") DO (
 IF "%%b"=="Server" ECHO %%c
 IF "%%b"=="MAC" ECHO %%c
 IF "%%b"=="http" ECHO %%b:%%c
 IF "%%b"=="00" ECHO %%c|FINDSTR /b "1a:79:" >NUL &IF NOT ERRORLEVEL 1 FOR /f "delims=;" %%e IN ("%%c") DO ECHO %%b:%%e
)

GOTO :EOF

读取每一行,使用空格和冒号作为分隔符,选择第一个标记(到 %%b),并将该行的其余部分(标记 *)选择到 %%c

对于 ServerMAC 行,仅对

http 开头的行回显 %%c,同时回显两个 %%b%%c,重新插入冒号。

对于以 00 开头的行,检查该行的其余部分是否以 1a:79: 开头,如果是,则使用 重新标记该行的其余部分; 作为分隔符。仅选择第一个标记(默认),并回显 00、冒号以及; 之前的行部分。

抱歉,我不清楚输出文件的要求。数据将简单地回显到屏幕上。您没有提供任何有关如何判断所需数据何时完整的信息,因此我无法为您提供终止批处理的代码。

@ECHO OFF
SETLOCAL
rem The following settings for the source directory & filenames 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 "filename1=%sourcedir%\q71207091.txt"

FOR /f "usebackqtokens=1*delims=: " %%b IN ("%filename1%") DO (
 IF "%%b"=="Server" ECHO %%c
 IF "%%b"=="MAC" ECHO %%c
 IF "%%b"=="http" ECHO %%b:%%c
 IF "%%b"=="00" ECHO %%c|FINDSTR /b "1a:79:" >NUL &IF NOT ERRORLEVEL 1 FOR /f "delims=;" %%e IN ("%%c") DO ECHO %%b:%%e
)

GOTO :EOF

Read each line, using both spaces and colons as delimiters, selecting the first token (to %%b) and the rest of the line (token *) to %%c.

For the lines Server and MAC, echo %%c only

for lines starting http, echo both %%b and %%c, re-inserting the colon.

for lines that start 00, check whether the remainder of the line starts 1a:79: and if it does, then retokenise the rest-of-line using ; as a delimiter. Select the first token only (default), and echo the 00, a colon and the part of the line that precedes the ;.

I'm sorry, but I'm not clear on the requirement for the output file. The data will simply be echoed to the screen. You've not provided any information on how to tell when the data you want is complete, so I can't provide you with code to terminate the batch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文