Bash将数据转换为csv

时间:2017-06-22 22:58:49

标签: string csv awk sed data-conversion

我有一些来自API调用的数据,下面有两行。

"serialNumber=5086", "deviceName=CSECHO002", "address=1.1.1.3", "deviceType=Network Switch", "parentNames, "tag1 (Site)=East Coast", "tag2 (Location)=DATA CENTRE 2", "tag3 (Rack)=RACK 06", "tag4 (Tag 4)=", "tag5 (Tag 5)=Xtraction", "locationName=GOLLZDGP01:Gold Coast DGEX", "dgeName=GOLLZDGP01", "isSuspended=false", "clearOnOk=false", "smartNotify=true", "showOnSummary=true", "accountSerialNumber=49", "accountName=Echo Ent", "isReadOnly=false", "isImported=false", "model=Nexus7000 C7010 (10 Slot) Chassis", "vendor=Cisco Systems", "rediscoveryEnabled=true", "rediscoveryFrequency=1440", "rediscoveryNewTestsAction=updateAndLog", "rediscoveryUpdatedTestsAction=updateAndLog", "rediscoveryDeletedTestsAction=logOnly", "applicationProfiles=none", "configBackupEnabled=true", "configBackupFrequency=1440", "processCollectionEnabled=false", "flapPreventionWaitCycles=-1", "comment=Cisco Nexus 7000 Series Switches
"serialNumber=5091", "deviceName=CSECHO001", "address=1.1.1.2", "deviceType=Network Switch", "parentNames, "tag1 (Site)=East Coast", "tag2 (Location)=DATA CENTRE", "tag3 (Rack)=RACK B5", "tag4 (Tag 4)=", "tag5 (Tag 5)=Xtraction", "locationName=GOLLZDGP01:Gold Coast DGEX", "dgeName=GOLLZDGP01", "isSuspended=false", "clearOnOk=false", "smartNotify=true", "showOnSummary=true", "accountSerialNumber=49", "accountName=Echo Ent", "isReadOnly=false", "isImported=false", "model=Nexus7000 C7010 (10 Slot) Chassis", "vendor=Cisco Systems", "rediscoveryEnabled=true", "rediscoveryFrequency=1440", "rediscoveryNewTestsAction=updateAndLog", "rediscoveryUpdatedTestsAction=updateAndLog", "rediscoveryDeletedTestsAction=logOnly", "applicationProfiles=none", "configBackupEnabled=true", "configBackupFrequency=1440", "processCollectionEnabled=false", "flapPreventionWaitCycles=0", "comment=Cisco Nexus 7000 Series Switches

如何以csv格式获取数据,而没有字段名称?

我已经走到了这一步,但它变得越来越混乱,我确信有一种简单的方法可以在=和下一个双引号或类似之间抓取字符串。

>     for LINE in $(/usr/bin/curl -b cookie_det -s --insecure 'https://my.url.com/api/rest/command/device.list?deviceName=CS*'); do
> echo $LINE | sed 's/,/\n/g' | awk -F"=" '{ print $2}'| sed 's/"//g' ;
> echo -------; ; done

2 个答案:

答案 0 :(得分:0)

假设模式完全如上所述,您可以在sed替换中使用子匹配来获得所需内容:

YYYY-MM-DD HH:MM:SS

答案 1 :(得分:0)

输入:foo.txt

"serialNumber=5086", "deviceName=CSECHO002", "address=1.1.1.3", "deviceType=Network Switch", "parentNames, "tag1 (Site)=East Coast", "tag2 (Location)=DATA CENTRE 2", "tag3 (Rack)=RACK 06", "tag4 (Tag 4)=", "tag5 (Tag 5)=Xtraction", "locationName=GOLLZDGP01:Gold Coast DGEX", "dgeName=GOLLZDGP01", "isSuspended=false", "clearOnOk=false", "smartNotify=true", "showOnSummary=true", "accountSerialNumber=49", "accountName=Echo Ent", "isReadOnly=false", "isImported=false", "model=Nexus7000 C7010 (10 Slot) Chassis", "vendor=Cisco Systems", "rediscoveryEnabled=true", "rediscoveryFrequency=1440", "rediscoveryNewTestsAction=updateAndLog", "rediscoveryUpdatedTestsAction=updateAndLog", "rediscoveryDeletedTestsAction=logOnly", "applicationProfiles=none", "configBackupEnabled=true", "configBackupFrequency=1440", "processCollectionEnabled=false", "flapPreventionWaitCycles=-1", "comment=Cisco Nexus 7000 Series Switches
"serialNumber=5091", "deviceName=CSECHO001", "address=1.1.1.2", "deviceType=Network Switch", "parentNames, "tag1 (Site)=East Coast", "tag2 (Location)=DATA CENTRE", "tag3 (Rack)=RACK B5", "tag4 (Tag 4)=", "tag5 (Tag 5)=Xtraction", "locationName=GOLLZDGP01:Gold Coast DGEX", "dgeName=GOLLZDGP01", "isSuspended=false", "clearOnOk=false", "smartNotify=true", "showOnSummary=true", "accountSerialNumber=49", "accountName=Echo Ent", "isReadOnly=false", "isImported=false", "model=Nexus7000 C7010 (10 Slot) Chassis", "vendor=Cisco Systems", "rediscoveryEnabled=true", "rediscoveryFrequency=1440", "rediscoveryNewTestsAction=updateAndLog", "rediscoveryUpdatedTestsAction=updateAndLog", "rediscoveryDeletedTestsAction=logOnly", "applicationProfiles=none", "configBackupEnabled=true", "configBackupFrequency=1440", "processCollectionEnabled=false", "flapPreventionWaitCycles=0", "comment=Cisco Nexus 7000 Series Switches

foo.sh

while read -r line ; do
    echo $line | sed -e 's/"[^=]*=\([^"]*\)"/"\1"/g'
done < foo.txt

输出:

"5086", "CSECHO002", "1.1.1.3", "Network Switch", "East Coast", "DATA CENTRE 2", "RACK 06", "", "Xtraction", "GOLLZDGP01:Gold Coast DGEX", "GOLLZDGP01", "false", "false", "true", "true", "49", "Echo Ent", "false", "false", "Nexus7000 C7010 (10 Slot) Chassis", "Cisco Systems", "true", "1440", "updateAndLog", "updateAndLog", "logOnly", "none", "true", "1440", "false", "-1", "Cisco Nexus 7000 Series Switches"
"5091", "CSECHO001", "1.1.1.2", "Network Switch", "East Coast", "DATA CENTRE", "RACK B5", "", "Xtraction", "GOLLZDGP01:Gold Coast DGEX", "GOLLZDGP01", "false", "false", "true", "true", "49", "Echo Ent", "false", "false", "Nexus7000 C7010 (10 Slot) Chassis", "Cisco Systems", "true", "1440", "updateAndLog", "updateAndLog", "logOnly", "none", "true", "1440", "false", "0", "Cisco Nexus 7000 Series Switches"