Search code examples
shellawksedcut

Strings between 2 characters unix


First I would like to apologies for posting similar thread which is present. But since I was not able to solve it hence I am posting it.

I have a wired string pattern ( mongo output) which I need to convert to only values.

"_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"

I want output like

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

I have tried various sed and awk but I am not able to achieve it .

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

Can someone help me with it ?

Many thanks in advance.


Solution

  • awk solution:

    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
    

    The output:

    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':|, *"' - field separator

    • for(i=2;i<=NF;i+=2) - iterating through all even fields

    • gsub(/^ *([^(]+\()?|"|\)$/,"",$i) - replace/remove excessive instance names like ObjectId( and double quotes "