saurav
saurav

Reputation: 219

Insert string to a file using sed

I have copied three columns to a file but need to change the format of one column (map).

         echo "copy admin.product (my_references, id, my_date) to 'updateProductStatement.cql';" > copyInputs.cql

Output file looks like:

,4.IM-H-506X,2016-01-01 11:07:27-0500
['LOWERCASETEST7'],JASONTEST7,2015-04-19 00:00:00-0400
"['EPROSP_IWS', '648099_EPROSP_IWS']",4.NDR-IWS-EPRO,2015-04-16 08:04:21-0400

After copying the data I tried the below command to separate all the columns:

    sed "s/' *, *'/' '/g;s/\([^,]*\),\([^,]*\),\(.*\)/update table set cola = \1 where colb = \2 and colc = \3/;s/' '/','/g" tempFile > updatestmt.cql

I get output like this:

    update table set cola = where colb = E2Bn9 and colc = 2015-04-29 00:00:00-0500

    update table set cola = ['2C173'] where colb = E2BA8 and colc = 2015-04-29 00:00:00-0500

    update table set cola = "['5A475' where colb =  '2C174'] and colc = E2BA8

Here I want to insert something in this format for my 1st column as {'my_refrences':''}.

So my expected output file will look like:

{"['EPROSP_IWS', '648099_EPROSP_IWS']":""},4.NDR-IWS-EPRO,2015-04-16 08:04:21-0400

Any help or siggestion? I am very much new to the scripting world.

Upvotes: 0

Views: 150

Answers (3)

Ed Morton
Ed Morton

Reputation: 203149

You only show one line of output for your 3 lines of input so idk if this does what you want for the lines you haven't shared with us but this may be what you want:

$ cat tst.awk
BEGIN { FS=OFS="," }
{
    cola = $0
    sub(/(,[^,]+){2}$/,"",cola)
    print "{" cola ":\"\"}", $(NF-1), $NF
}
$
$ awk -f tst.awk file
{:""},4.IM-H-506X,2016-01-01 11:07:27-0500
{['LOWERCASETEST7']:""},JASONTEST7,2015-04-19 00:00:00-0400
{"['EPROSP_IWS', '648099_EPROSP_IWS']":""},4.NDR-IWS-EPRO,2015-04-16 08:04:21-0400

The above builds on @LászlóSzilágyi's answer to your previous question - you really should accept that answer to that question as it is the clearest and simplest and is obviously trivial to enhance as your needs change.

In GNU sed btw you can get the output above with just:

$ sed -r 's/(.*)((,[^,]+){2})$/{\1:""}\2/' file
{:""},4.IM-H-506X,2016-01-01 11:07:27-0500
{['LOWERCASETEST7']:""},JASONTEST7,2015-04-19 00:00:00-0400
{"['EPROSP_IWS', '648099_EPROSP_IWS']":""},4.NDR-IWS-EPRO,2015-04-16 08:04:21-0400

but I suspect that's not really what you are trying to do and it's not as trivial as the awk script to modify if you want to do something different for the first line where the first field is empty or the second line that doesn't have double quotes around the first field.

Upvotes: 0

anishsane
anishsane

Reputation: 20970

I would normally prefer using gawk (compared to sed) for parsing of csv type data, due to its feature - splitting by content.

$ cat test.data
,4.IM-H-506X,2016-01-01 11:07:27-0500
['LOWERCASETEST7'],JASONTEST7,2015-04-19 00:00:00-0400
"['EPROSP_IWS', '648099_EPROSP_IWS']",4.NDR-IWS-EPRO,2015-04-16 08:04:21-0400

$ gawk '$1{$1="{"$1":\"\"}"; NF}1' FPAT='("[^"]*")|([^,]*)' OFS=, test.data
,4.IM-H-506X,2016-01-01 11:07:27-0500
{['LOWERCASETEST7']:""},JASONTEST7,2015-04-19 00:00:00-0400
{"['EPROSP_IWS', '648099_EPROSP_IWS']":""},4.NDR-IWS-EPRO,2015-04-16 08:04:21-0400

Upvotes: 1

SLePort
SLePort

Reputation: 15461

Try this :

sed "s/^\(\"*\[[^]]*\]\"*\)\(.*\)/{\1:\"\"}\2/" file

With file :

,4.IM-H-506X,2016-01-01 11:07:27-0500
['LOWERCASETEST7'],JASONTEST7,2015-04-19 00:00:00-0400
"['EPROSP_IWS', '648099_EPROSP_IWS']",4.NDR-IWS-EPRO,2015-04-16 08:04:21-0400

Output :

,4.IM-H-506X,2016-01-01 11:07:27-0500
{['LOWERCASETEST7']:""},JASONTEST7,2015-04-19 00:00:00-0400
{"['EPROSP_IWS', '648099_EPROSP_IWS']":""},4.NDR-IWS-EPRO,2015-04-16 08:04:21-0400

Upvotes: 1

Related Questions