2个字符之间的字符串unix

时间:2017-06-09 11:33:42

标签: shell awk sed cut

首先,我想对发布存在的类似帖子表示歉意。但由于我无法解决它,因此我发布了它。

我有一个有线字符串模式(mongo输出),我需要将其转换为仅值。

"_id" : ObjectId("59280d9b95385c78b73252e4"), "categorySetId" : NumberLong(1100000041), "categorySetName" : "PROD GROUP", "serviceableProductFlag" : "N", "categoryId" : NumberLong(1053), "pid" : "800-319-03", "productFamily" : "PP", "productType" : "SEATS", "subGroup" : "PP SER", "description" : "^AY,EECH, NG-C", "inventoryItemId" : NumberLong(200699), "itemStatusMfg" : "S-INTV", "organizationIdMfg" : NumberLong(90000), "src" : "orcl", "syncedOn" : NumberLong("1495797136138"), "CreationDate" : ISODate("2017-05-26T11:12:16.138Z"), "CreatedBy" : "tool", "LastUpdatedDate" : ISODate("2017-05-26T11:12:16.138Z"), "LastUpdatedBy" : "tool", "itemFamilyDesc" : "PP FAMILY", "itemFamilyGroupId" : 750, "itemFamilyGroupName" : "PP SERIES PRODUCTS"

我想输出

59280d9b95385c78b73252e4,1100000041,PROD GROUP,N,1053,800-319-03,PP, SEATS,PP SER ,'^AY,EECH, NG-C', 200699,S-INTV,90000,orcl,1495797136138,2017-05-26T11:12:16.138Z,tool,2017-05-26T11:12:16.138Z,tool,PP FAMILY,750,PP SERIES PRODUCTS

我尝试了各种sed和awk,但我无法实现它。

sed 's/"[^"]*://g' text.txt
sed 's/"[^:]*://g' text.txt

有人可以帮我吗?

非常感谢提前。

2 个答案:

答案 0 :(得分:1)

awk 解决方案:

awk -F': +|, *"' '{ r=""; for(i=2;i<=NF;i+=2) { 
         gsub(/^ *([^(]+\()?|"|\)$/,"",$i); 
         if(index($i,",")!=0){ $i="\047"$i"\047" } r=(r!="")? r","$i : $i 
     } print r }' text.txt

输出:

59280d9b95385c78b73252e4,1100000041,PROD GROUP,N,1053,800-319-03,PP,SEATS,PP SER,'^AY,EECH, NG-C',200699,S-INTV,90000,orcl,1495797136138,2017-05-26 11:12:16.138Z,tool,2017-05-26 11:12:16.138Z,tool,PP FAMILY,750,PP SERIES PRODUCTS
  • -F':|, *"' - 字段分隔符

  • for(i=2;i<=NF;i+=2) - 遍历所有甚至字段

  • gsub(/^ *([^(]+\()?|"|\)$/,"",$i) - 替换/删除过多的实例名称,例如ObjectId(和双引号"

答案 1 :(得分:0)

你的一些字段值嵌入了逗号,所以这很可能是徒劳的,但是......

#! /bin/sed -rf

s/\s*"[^"]+"\s*:\s*\w+\s*\("([^"]*)"\)\s*/\1/g
s/\s*"[^"]+"\s*:\s*\w+\s*\(([^)]*)\)\s*/\1/g
s/\s*"[^"]+"\s*:\s*"([^"]*)"\s*/\1/g
s/\s*"[^"]+"\s*:\s*([0-9]+)\s*/\1/g