如何使用 xargs 和 sed 将结果传送到数组?

如何使用 xargs 和 sed 将结果传送到数组?

我使用 curl 从 REST 端点提取 JSON,然后使用 node 包对其进行解析json获取一些 JSON 属性,稍后我会在一些应用程序配置中用到它们。

curl -s http://52.71.126.196/arcgis/rest/services/WaterNetwork/MapServer/0?f=pjson \
    | json -a fields \
    | json -a name

..这使:

OBJECTID
SHAPE
FACILITYID
ACCOUNTID
METSERVICE
SERVICETYPE
INSTALLDATE
LOCDESC
ROTATION
LOCATIONID
CRITICAL
ENABLED
ACTIVEFLAG
OWNEDBY
MAINTBY
LASTUPDATE
LASTEDITOR
BILLINGNAME
SERVICECODE
CYCLECODE
RATETABLE
SERVICESIZE
REMSERIALNUMBER
METERMULTIPLIER
LONGITUDE
LATITUDE
METERPULL

完美。但是,我真正需要的是将这些放入数组中,例如:

["OBJECTID", "SHAPE", "FACILITYID", "ACCOUNTID", "METSERVICE", 
 "SERVICETYPE", "INSTALLDATE", "LOCDESC", "ROTATION", "LOCATIONID", 
 "CRITICAL", "ENABLED", "ACTIVEFLAG", "OWNEDBY", "MAINTBY", "LASTUPDATE", 
 "LASTEDITOR", "BILLINGNAME", "SERVICECODE", "CYCLECODE", "RATETABLE",
 "SERVICESIZE", "REMSERIALNUMBER", "METERMULTIPLIER", "LONGITUDE", 
 "LATITUDE", "METERPULL"]

我找到了一个使用 xargs 和 sed 的命令,并对其进行了修改,以部分实现该目的:

curl -s http://52.71.126.196/arcgis/rest/services/WaterNetwork/MapServer/0?f=pjson \
    | json -a fields \
    | json -a name \
    | xargs echo \
    | sed 's/ /", "/g'

...这使:

OBJECTID", "SHAPE", "FACILITYID", "ACCOUNTID", "METSERVICE", "SERVICETYPE", 
"INSTALLDATE", "LOCDESC", "ROTATION", "LOCATIONID", "CRITICAL", "ENABLED", 
"ACTIVEFLAG", "OWNEDBY", "MAINTBY", "LASTUPDATE", "LASTEDITOR", 
"BILLINGNAME", "SERVICECODE", "CYCLECODE", "RATETABLE", "SERVICESIZE", 
"REMSERIALNUMBER", "METERMULTIPLIER", "LONGITUDE", "LATITUDE", "METERPULL

此外,我确实需要能够获取别名数据:

curl -s http://52.71.126.196/arcgis/rest/services/WaterNetwork/MapServer/0?f=pjson \
    | json -a fields \
    | json -a alias
OBJECTID
SHAPE
Facility Identifier
Account Number
Metered Service
Service Type
Install Date
Location Description
Rotation
Location Identifier
CriticalCustomer
Enabled
Active Flag
Owned By
Managed By
Last Update Date
Last Editor
BILLINGNAME
SERVICECODE
CYCLECODE
RATETABLE
SERVICESIZE
REMSERIALNUMBER
METERMULTIPLIER
LONGITUDE
LATITUDE
METERPULL

我的 xargs/sed 命令在这里崩溃,因为别名中有空格:

curl -s http://52.71.126.196/arcgis/rest/services/WaterNetwork/MapServer/0?f=pjson \
    | json -a fields \
    | json -a alias \
    | xargs echo \
    | sed 's/ /", "/g'

如您所见,它用空格分隔每个单词,这不是我需要的。

OBJECTID", "SHAPE", "Facility", "Identifier", "Account", "Number", 
"Metered", "Service", "Service", "Type", "Install", "Date", "Location", 
"Description", "Rotation", "Location", "Identifier", "CriticalCustomer", 
"Enabled", "Active", "Flag", "Owned", "By", "Managed", "By", "Last", 
"Update", "Date", "Last", "Editor", "BILLINGNAME", "SERVICECODE", 
"CYCLECODE", "RATETABLE", "SERVICESIZE", "REMSERIALNUMBER", 
"METERMULTIPLIER", "LONGITUDE", "LATITUDE", "METERPULL

...这就是我的 xargs/sed foo 失败的地方。我不确定这是 xargs 的问题还是 sed 的问题,以及我是否需要将正则表达式传递给 sed 来处理别名中可能存在的空格。是否有 xargs/sed 命令可以为上述数组形式中的名称和别名提供结果?

答案1

如果你有 python,一个简单的解决方案是:

curl -s http://52.71.126.196/arcgis/rest/services/WaterNetwork/MapServer/0?f=pjson \
    | json -a fields \
    | json -a alias \
    | python -c 'import sys;print([line.strip() for line in sys.stdin])'

这使:

['OBJECTID', 'SHAPE', 'Facility Identifier', 'Account Number', 'Metered Service', 'Service Type', 'Install Date', 'Location Description', 'Rotation', 'Location Identifier', 'CriticalCustomer', 'Enabled', 'Active Flag', 'Owned By', 'Managed By', 'Last Update Date', 'Last Editor', 'BILLINGNAME', 'SERVICECODE', 'CYCLECODE', 'RATETABLE', 'SERVICESIZE', 'REMSERIALNUMBER', 'METERMULTIPLIER', 'LONGITUDE', 'LATITUDE', 'METERPULL']

如果需要双引号:

curl -s http://52.71.126.196/arcgis/rest/services/WaterNetwork/MapServer/0?f=pjson \
    | json -a fields \
    | json -a alias \
    | python -c 'import sys;print([line.strip() for line in sys.stdin])' \
    | sed 's/'"'"'/"/g'

这使:

["OBJECTID", "SHAPE", "Facility Identifier", "Account Number", "Metered Service", "Service Type", "Install Date", "Location Description", "Rotation", "Location Identifier", "CriticalCustomer", "Enabled", "Active Flag", "Owned By", "Managed By", "Last Update Date", "Last Editor", "BILLINGNAME", "SERVICECODE", "CYCLECODE", "RATETABLE", "SERVICESIZE", "REMSERIALNUMBER", "METERMULTIPLIER", "LONGITUDE", "LATITUDE", "METERPULL"]

答案2

$ curl -s http://52.71.126.196/arcgis/rest/services/WaterNetwork/MapServer/0?f=pjson | json -a fields | json -a alias | sed 's/\(.*\)/["\1"]/g' | json -g

["\1"]是正则表达式捕获组,用引号和方括号括起来——因此您会得到一个数组列表,每个数组包含一个alias元素,如下所示:

["foo bar"] ["baz quux"]

然后json -g将所有这些迷你阵列分组为一个:

["foo bar", "baz quux"]

答案3

尝试使用perl

curl -s http://52.71.126.196/arcgis/rest/services/WaterNetwork/MapServer/0?f=pjson | json -a fields | json -a alias | perl -pe 's/\n/", "/g'

给出:

OBJECTID", "SHAPE", "Facility Identifier", "Account Number", "Metered Service", "Service Type", "Install Date", "Location Description", "Rotation", "Location Identifier", "CriticalCustomer", "Enabled", "Active Flag", "Owned By", "Managed By", "Last Update Date", "Last Editor", "BILLINGNAME", "SERVICECODE", "CYCLECODE", "RATETABLE", "SERVICESIZE", "REMSERIALNUMBER", "METERMULTIPLIER", "LONGITUDE", "LATITUDE", "METERPULL

相关内容