gpt4 book ai didi

linux - 在读取命令时保留带空格的字符串

转载 作者:太空宇宙 更新时间:2023-11-04 09:10:49 24 4
gpt4 key购买 nike

所以我有一个输出逗号分隔字符串的命令

喜欢这个;

"Name","Type","FC Ports","iSCSI Ports","User Group","Cluster","Performance Class"
"mnlas-esxq01","default","100000051EC20A95,100000051EC20A96,100000051EC21AC3,100000051EC21AC4","","","MNLASDMZ CL-1","none"
"mnlas-esxa02","default","100000051EC20D3A,100000051EC20D3B,100000051EC2218D,100000051EC2218E","","","MNLASDMZ CL-1","none"
"mnlas-esx916","default","21000024FF4DDC20,21000024FF4DDC21,21000024FF490CFC,21000024FF490CFD","","","MNLASPRODSCCM CL-1","none"
"mnlas-esx417","default","21000024FF4DDBE8,21000024FF4DDBE9,21000024FF490F56,21000024FF490F57","","","MNLASPRODSCCM CL-1","none"
"mnlas-esx111","default","21000024FF4DDC56,21000024FF4DDC57,21000024FF490F00,21000024FF490F01","","","MNLASPRODSCCM CL-1","none"
"mnlas-p-xca09.pr.int.ptc.com","default","21000024FF514CF4,21000024FF514CF5","","","MNLAS P-xca CL-1","none"
"mnlas-p-xca10.pr.int.ptc.com","default","21000024FF514ED2,21000024FF514ED3","","","MNLAS P-xca CL-1","none"
"mnlas-p-xca11.pr.int.ptc.com","default","21000024FF514FB6,21000024FF514FB7","","","MNLAS P-xca CL-1","none"
"mnlas-esx403.ad.cibc.com","default","10008C7CFF702490,10008C7CFF702491,10008C7CFF702494,10008C7CFF702495,10008C7CFF652604,10008C7CFF652605,10008C7CFF20D0A8,10008C7CFF20D0A9","","","MNLASPRODSQL CL-1","none"
"mnlas-esx401.ad.cibc.com","default","10008C7CFF702484,10008C7CFF702485,10008C7CFF7023F4,10008C7CFF7023F5,10008C7CFF21B3E8,10008C7CFF21B3E9,10008C7CFF65203E,10008C7CFF65203F","","","MNLASPRODSQL CL-1","none"

如您所见,长度也不同。

我的目标是回显名称、iSCSI 端口和集群,但有时某些字段也是空的。

我怎样才能正确格式化它并使用 grep 找到看起来像这样的主机名。

Name            iSCSI Ports                               Cluster
mnlas-esxq01 100000051EC20A95,100000051EC20A96 MNLASDMZ CL-1

顺便说一句,由于端口数量的原因,iSCSI 端口往往会更长一些,因此 Cluster 字段应该调整

我试过使用这个命令,但没有用。它说

sh-4.2# cat temp/testS | grep -i esx401 | while read "Name" "Type" "iSCSI Ports" "User Group" "Cluster" "Performance Class";do echo $Name $"iSCSI Ports" $Cluster;done
sh: read: `iSCSI Ports': not a valid identifier

我也试过将它定义为一个变量,但没有成功。

如有任何帮助,我们将不胜感激。

最佳答案

这里是“名称”的定义,shell 将其用于变量和函数名称:

name

A word consisting solely of letters, numbers, and underscores, and beginning with a letter or underscore. Names are used as shell variable and function names. Also referred to as an identifier.

2 Definitions

不允许在变量名中使用空格。

鉴于此,CSV 文件有时很难处理。非常棘手,您应该使用适当的 CSV 解析器。 是一套很好的工具:

csvcut -c "Name,iSCSI Ports,Cluster" file.csv

关于linux - 在读取命令时保留带空格的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56824548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com