找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 19797|回复: 5

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

[复制链接]
发表于 2023-11-20 07:19:45 | 显示全部楼层 |阅读模式
本帖最后由 不知道是谁 于 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
复制代码

评分

参与人数 1PB +2 收起 理由
Batcher + 2 感谢给帖子标题标注[已解决]字样

查看全部评分

发表于 2023-11-20 09:09:13 | 显示全部楼层
powershell
  1. ([regex]'(?<=name", ")(.*?@.*?)"[\s\S]+?password", "(.*?)(?="\);)').matches([io.file]::ReadAllText(".\test.js"))|%{"邮箱1:"+$_.Groups[1].Value+" 密码:"+$_.Groups[2].Value}
复制代码
发表于 2023-11-20 09:19:05 | 显示全部楼层
批处理文件保存为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
复制代码

评分

参与人数 1技术 +1 收起 理由
不知道是谁 + 1 乐于助人

查看全部评分

发表于 2023-11-20 10:43:44 | 显示全部楼层

假设一楼示例数据文件 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
复制代码

评分

参与人数 1技术 +1 收起 理由
不知道是谁 + 1 乐于助人

查看全部评分

发表于 2023-11-20 11:29:56 | 显示全部楼层
本帖最后由 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
复制代码

评分

参与人数 1技术 +1 收起 理由
不知道是谁 + 1 乐于助人

查看全部评分

发表于 2023-11-20 17:03:12 | 显示全部楼层
回复 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
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-18 08:53 , Processed in 0.023023 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表