标题: [文本处理] [已解决]批处理提取文本指定内容并且合并输出到新的文本里 [打印本页]
作者: a574045075 时间: 2024-4-20 11:01 标题: [已解决]批处理提取文本指定内容并且合并输出到新的文本里
你好!如何用批处理提取source_code.txt文件一些内容和链接,例如提取里面的/yuzu-emu-mirror/yuzu-mainline/releases/tag/mainline-0-1732(1732数值不是固定的,可能是其它数值,这样的链接一共10个),<relative-time datetime="2024-03-02(日期不是固定的,可能是其它日期,这样内容一共10个)和yuzu-emu-mirror/yuzu-mainline/commit/d839edbda1832cfaab8e4a06d96f7783cdb63e0b(这一长串代码也不是固定的,这样的链接一样也是10个),提取后把这3个链接或者内容合并为一行并输出到新的txt文本里,请问怎么处理,谢谢!!
source_code.txt
https://pan.baidu.com/s/19BPo0Ivor430A9yXRAGlTg?pwd=6666
处理后要的结果,以此类推,一样是10个- /yuzu-emu-mirror/yuzu-mainline/releases/tag/mainline-0-1732<relative-time datetime="2024-03-02/yuzu-emu-mirror/yuzu-mainline/commit/d839edbda1832cfaab8e4a06d96f7783cdb63e0b
- /yuzu-emu-mirror/yuzu-mainline/releases/tag/mainline-0-1731<relative-time datetime="2024-03-01/yuzu-emu-mirror/yuzu-mainline/commit/6879db1822c3c8be6e8598b3ce6762d1130b007e
- /yuzu-emu-mirror/yuzu-mainline/releases/tag/mainline-0-1730<relative-time datetime="2024-02-29/yuzu-emu-mirror/yuzu-mainline/commit/8dd5cd4eac9e47ee8722e2530d1db3b37bf73431
复制代码
作者: 77七 时间: 2024-4-20 13:37
- @echo off
- cd /d "%~dp0"
- (for /f usebacktokens^=1-6delims^=^" %%a in ("source_code.txt") do (
- if "%%a" equ " <h2 data-view-component=" (
- set /p="%%f"<nul
- ) else if "%%a" equ " <relative-time datetime=" (
- for /f "delims=T" %%x in ("%%b") do (
- set /p="<relative-time datetime=%%x"<nul
- )
- ) else if "%%a" equ " <a class=" (
- if "%%e" equ ">" (
- echo %%d
- )
- )
- ))>"out.txt"
- pause
复制代码
不知道怎么用set /p打印",我把"去掉了
作者: a574045075 时间: 2024-4-20 15:32
回复 2# 77七
你好 不行啊 输出内容如下:
D:\1>(if "<!DOCTYPE html>" EQU "聽 聽聽 聽<h2 data-view-component=" (set /p="" 0<nul ) else if "<!DOCTYPE html>" EQU "聽 聽聽 聽聽聽<relative-time datetime=" (for /F "delims=T" %x in ("") do (set /p="<relative-time datetime=%x" 0<nul ) ) else if "<!DOCTYPE html>" EQU "聽 聽聽 聽<a class=" (if "" EQU ">" (echo ) ) )
D:\1>(if "<html" EQU "聽 聽聽 聽<h2 data-view-component=" (set /p="" 0<nul ) else if "<html" EQU "聽 聽聽 聽聽聽<relative-time datetime=" (for /F "delims=T" %x in ("") do (set /p="<relative-time datetime=%x" 0<nul ) ) else if "<html" EQU "聽 聽聽 聽<a class=" (if "" EQU ">" (echo ) ) )
D:\1>(if " lang=" EQU "聽 聽聽 聽<h2 data-view-component=" (set /p="" 0<nul ) else if " lang=" EQU "聽 聽聽 聽聽聽<relative-time datetime=" (for /F "delims=T" %x in ("en") do (set /p="<relative-time datetime=%x" 0<nul ) ) else if " lang=" EQU "聽 聽聽 聽<a class=" (if "" EQU ">" (echo ) ) )
D:\1>(if " " EQU "聽 聽聽 聽<h2 data-view-component="
作者: 77七 时间: 2024-4-20 15:50
回复 3# a574045075
2楼代码点copy 按钮复制,代码保存为ansi或者utf-8,再试一下。我这里测试正常。
作者: a574045075 时间: 2024-4-20 15:56
回复 4# 77七
运行后如下 对了 你电脑是什么系统
D:\1>锘緻echo off
'锘緻echo' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
D:\1>cd /d "D:\1\"
D:\1>(for /F usebacktokens=1-6delims=" %a in ("source_code.txt") do (if "%a" EQU
" <h2 data-view-component=" (set /p="%f" 0<nul ) else if "%a" EQU "
<relative-time datetime=" (for /F "delims=T" %x in ("%b") do (set /p="<relativ
e-time datetime=%x" 0<nul ) ) else if "%a" EQU " <a class=" (if "%e" EQU "
>" (echo %d ) ) ) ) 1>"out.txt"
D:\1>pause
请按任意键继续. . .
作者: 77七 时间: 2024-4-20 16:06
回复 5# a574045075
win10
作者: hfxiang 时间: 2024-4-20 17:27
回复 1# a574045075
第3方工具gawk( http://bcn.bathome.net/tool/4.1.0/gawk.exe )的实现方式如下:- gawk "/\/yuzu-emu-mirror\/yuzu-mainline\/releases\/tag\/mainline-0-[0-9]{4}/{printf substr($0,67,59)}/relative-time datetime=/{printf substr($0,9,35)}/\/yuzu-emu-mirror\/yuzu-mainline\/commit\//{print substr($0,36,78)}" source_code.txt>out.txt
复制代码
作者: a574045075 时间: 2024-4-20 17:35
回复 6# 77七
可以用了 忘记说了 能不能把commit/后面的这串代码d839edbda1832cfaab8e4a06d96f7783cdb63e0b,我想保留前9位,也就是d839edbda,并在后面加入.zip
最后结果:
/yuzu-emu-mirror/yuzu-mainline/releases/tag/mainline-0-1732<relative-time datetime="2024-03-02/yuzu-emu-mirror/yuzu-mainline/commit/d839edbda.zip
作者: 77七 时间: 2024-4-20 17:56
回复 8# a574045075
2楼代码第12行改为:
- set str=%%d
- setlocal enabledelayedexpansion
- echo !str:~0,47!.zip
- endlocal
复制代码
作者: aloha20200628 时间: 2024-4-20 18:02
本帖最后由 aloha20200628 于 2024-4-20 18:10 编辑
回复 8# a574045075
也来切几刀...
用楼主提供的示例文件测试通过...- @echo off &setlocal enabledelayedexpansion
- set "s1=/yuzu-emu-mirror/yuzu-mainline/releases/tag/"
- set "s2=relative-time datetime="
- set "s3=/yuzu-emu-mirror/yuzu-mainline/commit/"
- (for /f tokens^=1-6^delims^=^" %%1 in (' findstr /c:"%s1%" /c:"%s2%" /c:"%s3%" "source_code.txt" ') do (
- set "s=%%~2" &set "c=!s:~,4!"
- if /i "!c!"=="true" (set "v=%%~6") else if /i "!c!"=="link" (set "v4=%%~4"&echo,!v!!v4:~,-31!.zip&set "v=") else (set "v=!v!<relative-time datetime=%%~2"&set "v=!v:~,-10!")
- ))>"source_code.txt.new"
- pause&exit/b
复制代码
作者: qixiaobin0715 时间: 2024-4-24 15:16
本帖最后由 qixiaobin0715 于 2024-4-24 17:07 编辑
- @echo off
- setlocal enabledelayedexpansion
- (for /f "delims=" %%i in ('findstr /l /c:"/yuzu-emu-mirror/yuzu-mainline/releases/tag" /l /c:"<relative-time datetime=" /l /c:"yuzu-emu-mirror/yuzu-mainline/commit" source_code.txt') do (
- set "str1=%%i"
- set "str1=!str1:>=!"
- for %%j in (!str1!) do (
- if "!s!"=="href" (
- if not defined n (
- set "str=%%~j"
- set n=true
- ) else (
- echo,!str!%%~j
- set n=
- )
- ) else if "!s!"=="datetime" (
- set "str=!str!<relative-time datetime=%%~j"
- )
- set s=%%j
- )
- ))>out.txt
- pause
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |