标题: [文本处理] 【已解决】求助批处理在文件中查找并提取指定内容生成文本文件 [打印本页]
作者: 不知道是谁 时间: 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
命令希望尽量简练些,比如不带文字描述的命令- for /f tokens^=4^ delims^=^" %%i in ('findstr "c.*\.email c.*\.password" "test.js"') do echo %%i
复制代码
作者: pd1 时间: 2023-11-20 09:09
powershell- ([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编码- @echo off
- setlocal enabledelayedexpansion
- for /f "tokens=2 delims=) " %%i in ('findstr "email password" "test.js"') do (
- set /a n+=1
- set /a x=n%%2
- set /a "y=(n-1)/2+1"
- if !x! equ 1 (
- echo,邮箱地址!y!: %%~i
- ) else (
- echo,邮箱密码!y!: %%~i
- )
- )
- pause
复制代码
作者: aloha20200628 时间: 2023-11-20 10:43
假设一楼示例数据文件 test.js 被存为简中编码,以下批处理脚本亦须存为简中编码。- @echo off &setlocal enabledelayedexpansion &set "n=1"
- (for /f "delims=" %%a in ('findstr /i "email password" test.js') do (
- set "b=%%a" &set "e=!b:*email=!" &set "p=!b:*password=!"
- if /i "!b!" neq "!e!" (echo,邮箱地址!n!: !e:~4,-3!)
- if /i "!b!" neq "!p!" (echo,邮箱密码!n!: !p:~4,-3!&set/a n+=1)
- ))>test.new
- endlocal&exit/b
复制代码
作者: qixiaobin0715 时间: 2023-11-20 11:29
本帖最后由 qixiaobin0715 于 2023-11-20 14:05 编辑
这样也行:- @echo off
- setlocal enabledelayedexpansion
- for /f "tokens=2,3 delims=(), " %%i in (test.js) do (
- if "%%~xi"==".email" (
- set /a n+=1
- echo,邮箱地址!n!: %%~j
- ) else if "%%~xi"==".password" (
- echo,邮箱密码!n!: %%~j
- )
- )
- pause
复制代码
邮箱及密码在同一行显示:- @echo off
- setlocal enabledelayedexpansion
- for /f "tokens=2,3 delims=(), " %%i in (test.js) do (
- if "%%~xi"==".email" (
- set /a n+=1
- set "Email=邮箱!n!: %%~j"
- ) else if "%%~xi"==".password" (
- echo,!Email! 密码: %%~j
- )
- )
- pause
复制代码
作者: hfxiang 时间: 2023-11-20 17:03
回复 1# 不知道是谁
用第3方工具gawk( http://bcn.bathome.net/tool/4.1.0/gawk.exe )实现方式如下:- 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://www.bathome.net/) |
Powered by Discuz! 7.2 |