Board logo

标题: [文本处理] 【已解决】求助批处理在文件中查找并提取指定内容生成文本文件 [打印本页]

作者: 不知道是谁    时间: 2023-11-20 07:19     标题: 【已解决】求助批处理在文件中查找并提取指定内容生成文本文件

本帖最后由 不知道是谁 于 2023-11-20 17:26 编辑

假设文本内容如下:
user_pref("app.load.messenger", true);
user_pref("app.update.lastUpdateTime.addon-background-update-timer", 1698398230);
user_pref("app.update.lastUpdateTime.blocklist-background-update-timer", 1698398350);
user_pref("app.update.lastUpdateTime.search-engine-update-timer", 1698373978);
user_pref("app.update.lastUpdateTime.xpi-signature-verification", 1698398445);
user_pref("browser.cache.disk.capacity", 358400);
user_pref("browser.cache.disk.filesystem_reported", 1);
user_pref("browser.download.importedFromSqlite", true);
user_pref("calendar.list.sortOrder", "9068e928-44b0-47ef-9409-398c83841143 1bee8ac2-2872-439a-9227-8d3b729eaaf2");
user_pref("calendar.registry.1bee8ac2-2872-439a-9227-8d3b729eaaf2.cache.enabled", true);
user_pref("calendar.registry.1bee8ac2-2872-439a-9227-8d3b729eaaf2.calendar-main-in-composite", true);
user_pref("calendar.registry.1bee8ac2-2872-439a-9227-8d3b729eaaf2.color", "#a8c2e1");
user_pref("calendar.registry.1bee8ac2-2872-439a-9227-8d3b729eaaf2.email", "a@qq.com");
user_pref("calendar.registry.1bee8ac2-2872-439a-9227-8d3b729eaaf2.imip.identity.key", "id1");
user_pref("calendar.registry.1bee8ac2-2872-439a-9227-8d3b729eaaf2.name", "a@qq.com");
user_pref("calendar.registry.1bee8ac2-2872-439a-9227-8d3b729eaaf2.password", "a.123456");
user_pref("calendar.registry.9068e928-44b0-47ef-9409-398c83841143.email", "b@qq.com");
user_pref("calendar.registry.9068e928-44b0-47ef-9409-398c83841143.pop3.identity.key", "id2");
user_pref("calendar.registry.9068e928-44b0-47ef-9409-398c83841143.name", "b@qq.com");
user_pref("calendar.registry.9068e928-44b0-47ef-9409-398c83841143.password", "b.123456");
user_pref("calendar.registry.3ecdadd0-7b89-410f-9a27-1c3fff3fba9a.email", "c@qq.com");
user_pref("calendar.registry.3ecdadd0-7b89-410f-9a27-1c3fff3fba9a.pop3.identity.key", "id3");
user_pref("calendar.registry.3ecdadd0-7b89-410f-9a27-1c3fff3fba9a.name", "c@qq.com");
user_pref("calendar.registry.3ecdadd0-7b89-410f-9a27-1c3fff3fba9a.password", "c.123456");
******more

需要提取其中的email和password,并输出文本内容如下:
邮箱地址1:a@qq.com
邮箱密码1:a.123456
邮箱地址2:b@qq.com
邮箱密码2:b.123456
邮箱地址3:c@qq.com
邮箱密码3:c.123456

或者
邮箱1:a@qq.com 密码:a.123456
邮箱2:b@qq.com 密码:b.123456
邮箱3:c@qq.com 密码:c.123456

命令希望尽量简练些,比如不带文字描述的命令
  1. for /f tokens^=4^ delims^=^" %%i in ('findstr "c.*\.email c.*\.password" "test.js"') do echo %%i
复制代码

作者: pd1    时间: 2023-11-20 09:09

powershell
  1. ([regex]'(?<=name", ")(.*?@.*?)"[\s\S]+?password", "(.*?)(?="\);)').matches([io.file]::ReadAllText(".\test.js"))|%{"邮箱1:"+$_.Groups[1].Value+" 密码:"+$_.Groups[2].Value}
复制代码

作者: qixiaobin0715    时间: 2023-11-20 09:19

批处理文件保存为ANSI编码
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "tokens=2 delims=) " %%i in ('findstr "email password" "test.js"') do (
  4.     set /a n+=1
  5.     set /a x=n%%2
  6.     set /a "y=(n-1)/2+1"
  7.     if !x! equ 1 (
  8.         echo,邮箱地址!y!: %%~i
  9.     ) else (
  10.         echo,邮箱密码!y!: %%~i
  11.     )
  12. )
  13. pause
复制代码

作者: aloha20200628    时间: 2023-11-20 10:43


假设一楼示例数据文件 test.js 被存为简中编码,以下批处理脚本亦须存为简中编码。
  1. @echo off &setlocal enabledelayedexpansion &set "n=1"
  2. (for /f "delims=" %%a in ('findstr /i "email password" test.js') do (
  3. set "b=%%a" &set "e=!b:*email=!" &set "p=!b:*password=!"
  4. if /i "!b!" neq "!e!" (echo,邮箱地址!n!: !e:~4,-3!)
  5. if /i "!b!" neq "!p!" (echo,邮箱密码!n!: !p:~4,-3!&set/a n+=1)
  6. ))>test.new
  7. endlocal&exit/b
复制代码

作者: qixiaobin0715    时间: 2023-11-20 11:29

本帖最后由 qixiaobin0715 于 2023-11-20 14:05 编辑

这样也行:
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "tokens=2,3 delims=(), " %%i in (test.js) do (
  4.     if "%%~xi"==".email" (
  5.         set /a n+=1
  6.         echo,邮箱地址!n!: %%~j
  7.     ) else if "%%~xi"==".password" (
  8.         echo,邮箱密码!n!: %%~j
  9.     )
  10. )
  11. pause
复制代码
邮箱及密码在同一行显示:
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. for /f "tokens=2,3 delims=(), " %%i in (test.js) do (
  4.     if "%%~xi"==".email" (
  5.         set /a n+=1
  6.         set "Email=邮箱!n!: %%~j"
  7.     ) else if "%%~xi"==".password" (
  8.         echo,!Email! 密码: %%~j
  9.     )
  10. )
  11. pause
复制代码

作者: hfxiang    时间: 2023-11-20 17:03

回复 1# 不知道是谁

用第3方工具gawk( http://bcn.bathome.net/tool/4.1.0/gawk.exe )实现方式如下:
  1. gawk "/email/,/password/{A=gensub(/^.+\042([^\042]+)\042\051;$/,\"\\1\",\"g\");if(/email/){email=A}if(/password/){printf\"邮箱\045d:\045s  密码:\045s\n\",++i,email,A}}" test.js>eMail_List.txt
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2