Adding quotes to variating characters in bash

47 Views Asked by At

I am trying to use the sed function in order to add double quotes for anything in between a matched pattern and a comma to break of the pattern. At the moment I am extracting the following data from cloudflare and I am trying to modify it to line protocol;

count=24043,clientIP=x.x.x.x,clientRequestPath=/abc/abs/abc.php
count=3935,clientIP=y.y.y.y,clientRequestPath=/abc/abc/abc/abc.html
count=3698,clientIP=z.z.z.z,clientRequestPath=/abc/abc/abc/abc.html

I have already converted to this format from JSON output with a bunch of sed functions to modify it, however, I am unable to get to the bottom of it to put the data for clientIP and clientRequestPath in inverted commas.

My expected output has to be;

count=24043,clientIP="x.x.x.x",clientRequestPath="/abc/abs/abc.php"
count=3935,clientIP="y.y.y.y",clientRequestPath="/abc/abc/abc/abc.html"
count=3698,clientIP="z.z.z.z",clientRequestPath="/abc/abc/abc/abc.html"

This data will be imported into InfluxDB, count will be a float whilst clientIP and clientRequestPath will be strings, hence why I need them to be in inverted commas as at the moment I am getting errors since they arent as they should be.

Is anyone available to provided to adequate 'sed' function to do is?

2

There are 2 best solutions below

1
On BEST ANSWER

This might work for you (GNU sed):

sed -E 's/=([^0-9][^,]*)/="\1"/g' file

Enclose any string following a = does not begin with a integer upto a , in double quotes, globally.

0
On

here is a solution using a SED script to allow for multiple operations on a source file.

assuming your source data is in a file "from.dat"

create a sed script to run multiple commands

cat script.sed
s/clientIP=/clientIP=\"/
s/,clientRequestPath/\",clientRequestPath/

execute multiple-command sed script on data file redirecting the output file "to.dat"

sed -f script.sed from.dat > to.dat

cat to.dat  (only showing one line)
count=24043,clientIP="x.x.x.x",clientRequestPath=/abc/abs/abc.php