如何在批处理脚本的文本文件中找到以下文本?

时间:2019-06-12 01:18:46

标签: bash

我一直在撞墙,试图解决这个问题。

{
"FleetAttributes": {
    "FleetId": "fleet-ad6061f3-6eaa-4a07-b68e-1042f1288e05",
    "FleetArn": "arn:aws:gamelift:us-west-2:321110313936:fleet/fleet-ad6061f3-6eaa-4a07-b68e-1042f1288e05",
    "FleetType": "ON_DEMAND",
    "InstanceType": "c4.large",
    "Description": "Development build of Project Genesis",
    "Name": "ProjectGenesis-DevelopmentBuild",
    "CreationTime": 1560297730.139,
    "Status": "NEW",
    "BuildId": "build-0f8b9c4f-56a3-48e3-af28-018383af8fd1",
    "NewGameSessionProtectionPolicy": "FullProtection",
    "OperatingSystem": "WINDOWS_2012",
    "ResourceCreationLimitPolicy": {
        "NewGameSessionsPerCreator": 4,
        "PolicyPeriodInMinutes": 15
    },
    "MetricGroups": [
        "default"
    ]
}

以上是文本文件的内容。我试图在批处理脚本中提取FleetId以将其放入变量中,以便以后可以将其用于其他命令。

我曾经尝试过findstr和regex,但是它的功能如此有限,因此显得毫无用处。

我需要找到fleet-ad6061f3-6eaa-4a07-b68e-1042f1288e05文本并将其放入var中的哪些选项?

Regex

上面的正则表达式示例足以找到它,但是我不能将它与findstr一起使用,因为它对扩展字符的支持不佳。

4 个答案:

答案 0 :(得分:0)

因为您在评论中询问了bash脚本的样子:

for thing in `grep 'FleetId' yourfile.txt | awk -F ':' '{ print $2 }' | tr -d '" ,'`; do
   # do something with the id as $thing
   echo $thing
done

答案 1 :(得分:0)

您的输入看起来像json-如果是json,那么您应该像jq那样使用json解析器,例如通过仅添加一个终止}将示例输入文件固定为有效json后:

$ cat file
{
"FleetAttributes": {
    "FleetId": "fleet-ad6061f3-6eaa-4a07-b68e-1042f1288e05",
    "FleetArn": "arn:aws:gamelift:us-west-2:321110313936:fleet/fleet-ad6061f3-6eaa-4a07-b68e-1042f1288e05",
    "FleetType": "ON_DEMAND",
    "InstanceType": "c4.large",
    "Description": "Development build of Project Genesis",
    "Name": "ProjectGenesis-DevelopmentBuild",
    "CreationTime": 1560297730.139,
    "Status": "NEW",
    "BuildId": "build-0f8b9c4f-56a3-48e3-af28-018383af8fd1",
    "NewGameSessionProtectionPolicy": "FullProtection",
    "OperatingSystem": "WINDOWS_2012",
    "ResourceCreationLimitPolicy": {
        "NewGameSessionsPerCreator": 4,
        "PolicyPeriodInMinutes": 15
    },
    "MetricGroups": [
        "default"
    ]
}
}

并将jq封装在shell脚本中,然后根据需要将其输出保存在变量中:

$ cat tst.sh
#!/bin/bash

tag="$1"
file="$2"

var=$(jq --arg tag "$tag" -r '.FleetAttributes[$tag]' "$file")

printf '%s\n' "$var"

$ ./tst.sh FleetId file
fleet-ad6061f3-6eaa-4a07-b68e-1042f1288e05

$ ./tst.sh BuildId file
build-0f8b9c4f-56a3-48e3-af28-018383af8fd1

否则,如果您输入的不是真正的json或无法安装jq之类的工具来理解json,那么awk将是您的下一个最佳选择:

$ cat tst.sh
#!/bin/bash

tag="$1"
file="$2"

var=$(awk -v tag="$tag" '$1=="\""tag"\":"{sub(/",?$/,""); sub(/.*"/,""); print}' "$file")

printf '%s\n' "$var"

$ ./tst.sh FleetId file
fleet-ad6061f3-6eaa-4a07-b68e-1042f1288e05

$ ./tst.sh BuildId file
build-0f8b9c4f-56a3-48e3-af28-018383af8fd1

但这需要进行重大更改才能实现:

$ jq -r .FleetAttributes.ResourceCreationLimitPolicy file
{
  "NewGameSessionsPerCreator": 4,
  "PolicyPeriodInMinutes": 15
}

$ jq -r .FleetAttributes.MetricGroups file
[
  "default"
]

答案 2 :(得分:0)

这适用于Windows cmd,

FOR /F "tokens=2 delims=:," %A IN ('findstr "FleetId" [name_json_file.json]') DO echo %A

在批处理文件中,

@echo off

FOR /F "tokens=2 delims=:," %%A IN ('findstr "FleetId" [name_json_file.json]') DO echo %%A

pause
::

答案 3 :(得分:-1)

我修改了@PaulProgrammer的建议以允许有参数。

for id in `grep "$1" $2 | awk -F ':' '{ print $2 }' | tr -d '" ,'`; do
   echo $id >> "$1"Temp.txt
done

这使我可以按如下方式传递2个参数。

GameLiftIdHelper.sh FleetId TestFleetId.txt
GameLiftIdHelper.sh "Build ID" TestBuildId.txt