0

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.

3
  • Can you show some effort and what you have already tried? Commented May 7, 2016 at 8:02
  • Sir i updated my post with my efforts i tired to solve my problem Commented May 7, 2016 at 8:10
  • By only showing us the desired output for 1 of your 3 input lines and by showing us a sed script and saying it produces output that it does not produce, and then saying you want one output format in your question but saying you actually want a different output format in a comment, you're making it very difficult for us to understand what you want so we can help you. edit your question to at the very least simply show the real expected output for all 3 lines of your posted sample input. Commented May 7, 2016 at 14:58

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

5 Comments

@user5655807 You're welcome. You may accept the answer if it helped.
the regex can be improved to reduce some backslashes: sed -r 's/^("*\[[^]]*\]"*)(.*)/{\1:""}\2/'
@spasic I used BRE for broader compatibility.
@Kenavoz - After getting this output am trying to prepare an update statement- sed "s/' *, *'/' '/g;s/([^,]*),([^,]*),(.*)/update table set cross_refs = \1 where id = \2 and effective_date = \3/;s/' '/','/g" tempFile > updatestmt.cql i need to update this statement according to your changes. can you help me in to do that?
@user5655807 It should work as is (with a -r option) or as described here, in BRE mode : stackoverflow.com/a/37056397/1344961.
1

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.