如何从文本文件解析用户名并将结果批量输出到另一个文本文件?

问题描述 投票:0回答:1

我正在重新发布此内容,因为有人没有时间来回答我的上一个问题,我没有时间进行适当的编辑。我醒来发现它已经关闭。 :C另外,我是这个网站的新手。

我不确定如何完成我想做的事情。使用批处理会使我感到更舒适。因为,这是我更熟悉的一种编程语言。请仅批处理脚本答复。

我需要将用户名(即elocin_anagram LuckeaterVR)解析为一个列表,并用逗号分隔成另一个文本文件。用户名是字符串中的显示名。该字符串位于名为subscriptions_first = 100.txt的文本文件中

这是字符串:

{"_total":19,"subscriptions":[{"created_at":"2018-06-15T19:34:38Z","_id":"b7c42f6ce857162220e99533d3d6dc1ae11fac8d","sub_plan":"3000","sub_plan_name":"Channel Sub (❤ω❤)♡ ♡ ♡(elocin_anagram)","is_gift":false,"user":{"display_name":"elocin_anagram","type":"user","bio":"personal bio here.","created_at":"2015-06-17T05:37:38Z","updated_at":"2020-05-11T05:51:58Z","name":"elocin_anagram","_id":"93742615","logo":"https://static-cdn.jtvnw.net/jtv_user_pictures/d37d128b-59b1-4015-9776-74866feb1d44-profile_image-300x300.png"},"sender":null},{"created_at":"2019-07-10T00:04:45Z","_id":"6a26c5a56b39d142a6e25ad30589a1b923fbc1bb","sub_plan":"1000","sub_plan_name":"Channel Sub(≧◡≦) ♡ (elocin_anagram) ","is_gift":false,"user":{"display_name":"LuckeaterVR","type":"user","bio":"","created_at":"2018-12-08T04:55:48Z","updated_at":"2020-04-24T01:44:56Z","name":"luckeatervr","_id":"400728304","logo":"https://static-cdn.jtvnw.net/jtv_user_pictures/322ba52a-655c-42a4-8cc9-7b875debd5dd-profile_image-300x300.png"},"sender":null},{"created_at":"2020-01-16T01:23:17Z","_id":"17704f74767b5592c5fc221eca11a20579a8162c","sub_plan":"3000","sub_plan_name":"Channel Sub (❤ω❤)♡ ♡ 

我需要文本文件中字符串的输出看起来像这样:

elocin_anagram,LuckeaterVR,用户名3,用户名4,用户名5,...用户名100

省略号(...)表示文件中可能有100个用户名。

这是我第一次尝试学习如何从文本文件中解析信息,并将其输出到另一个文本文件。我不确定如何做到这一点,并且非常感谢您提供一些指导,因此我可以学习。我尝试查看各种教程和文档。他们都没有向我展示如何获得期望的结果。用汤姆·斯科特(Tom Scott)的话说:“一旦知道了事物的名称,就可以在Google上搜索它。”也许我没有使用正确的搜索词。 ¯_(ツ)_ /¯(耸肩)

如果此帖子有任何问题,请允许我在24小时内修复它,然后再关闭它。自检疫以来,我的睡眠时间表很奇怪。另外,如何删除以前的帖子???

谢谢你。

parsing batch-file batch-processing
1个回答
0
投票
@echo off
setlocal

set "file=subscriptions_first=100.txt"
set "output=subscriptions_first=100_display_name.txt"

(    for /f "delims=" %%A in (
        'powershell -noprofile -command
        "$content= get-content -path '%file%' -raw | ConvertFrom-json;"
        "foreach ($subscription in $content.subscriptions) {"
        "$subscription.user.display_name}"'
    ) do echo %%~A
) > "%output%"

要获取display_name值,可以使用Powershell的convertfrom-json

名为file的环境变量设置为文本文件的路径。 Powershell将文件的get-content传送到convertfrom-json。从创建的对象访问每个user以获取display_name的值。回显的显示名称将重定向到由环境变量output设置的指定文件。

使用此极简JSON文件修饰的结构进行了测试。

{
    "subscriptions": [
        {
            "user": {
                "display_name": "elocin_anagram"
            }
        },
        {
            "user": {
                "display_name": "LuckeaterVR"
            }
        }
    ]
}

输出:

elocin_anagram
LuckeaterVR
© www.soinside.com 2019 - 2024. All rights reserved.