Replace data between Square Brackets and ignore other occurences

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have a csv file with below data



Input



"01","5","Male","[""No.**"",""**kg""]","","","" 
"02","6","FeMale","[""No.""]","","",""


I would like to replace the "," string which is between the square brackets but not the data outside the square brackets.



Output:



"01","5","Male","[""No.**,**kg""]","","","" 
"02","6","FeMale","[""No.""]","","",""


I tried below command, but it does replace data outside the square brackets, but not for all lines.



sed '/[/,/]/s/"",""/,/'






share|improve this question




















  • seems to me it's Python dictionary output ?
    – Î±Ô‹É±Ò½Ôƒ αмєяιcαη
    Dec 24 '17 at 19:08














up vote
1
down vote

favorite












I have a csv file with below data



Input



"01","5","Male","[""No.**"",""**kg""]","","","" 
"02","6","FeMale","[""No.""]","","",""


I would like to replace the "," string which is between the square brackets but not the data outside the square brackets.



Output:



"01","5","Male","[""No.**,**kg""]","","","" 
"02","6","FeMale","[""No.""]","","",""


I tried below command, but it does replace data outside the square brackets, but not for all lines.



sed '/[/,/]/s/"",""/,/'






share|improve this question




















  • seems to me it's Python dictionary output ?
    – Î±Ô‹É±Ò½Ôƒ αмєяιcαη
    Dec 24 '17 at 19:08












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a csv file with below data



Input



"01","5","Male","[""No.**"",""**kg""]","","","" 
"02","6","FeMale","[""No.""]","","",""


I would like to replace the "," string which is between the square brackets but not the data outside the square brackets.



Output:



"01","5","Male","[""No.**,**kg""]","","","" 
"02","6","FeMale","[""No.""]","","",""


I tried below command, but it does replace data outside the square brackets, but not for all lines.



sed '/[/,/]/s/"",""/,/'






share|improve this question












I have a csv file with below data



Input



"01","5","Male","[""No.**"",""**kg""]","","","" 
"02","6","FeMale","[""No.""]","","",""


I would like to replace the "," string which is between the square brackets but not the data outside the square brackets.



Output:



"01","5","Male","[""No.**,**kg""]","","","" 
"02","6","FeMale","[""No.""]","","",""


I tried below command, but it does replace data outside the square brackets, but not for all lines.



sed '/[/,/]/s/"",""/,/'








share|improve this question











share|improve this question




share|improve this question










asked Dec 24 '17 at 18:49









Harish

335




335











  • seems to me it's Python dictionary output ?
    – Î±Ô‹É±Ò½Ôƒ αмєяιcαη
    Dec 24 '17 at 19:08
















  • seems to me it's Python dictionary output ?
    – Î±Ô‹É±Ò½Ôƒ αмєяιcαη
    Dec 24 '17 at 19:08















seems to me it's Python dictionary output ?
– Î±Ô‹É±Ò½Ôƒ αмєяιcαη
Dec 24 '17 at 19:08




seems to me it's Python dictionary output ?
– Î±Ô‹É±Ò½Ôƒ αмєяιcαη
Dec 24 '17 at 19:08










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










sed '/[/,/]/s/"",""/,/'


This would look for a line with a [, then a line with a ], and between those lines, replace the first "","" encountered on each line. It doesn't really look at where the [..] are within a line.




As a zero-order try, something like this:



$ cat x
"01","5","Male","[""No.**"",""**kg""]","","",""
$ sed 's/([.*)"",""(.*])/1,2/g' x
"01","5","Male","[""No.**,**kg""]","","",""


The pattern matches a [, anything, "","", anything and a ], while capturing all but the "","" so it can put the pieces back together.



This will break for stuff like [..],"","",[..] (where the brackets close before a "","" is seen, the pattern searches for a following ]) and [.."","".."",""..] (with multiple "","" sequences inside the brackets, only one is removed).



Slightly more genarally with Perl, though this is a horrible substitution-within-substitution trick. You should probably use a proper parser instead:



$ cat y
no removal here: [...],"","",[...]
double removal here: [ "","" "","" ]
[""remove"",""here""],""not"",""here"",[""also"",""here""]

$ perl -pe 'sub x $a = shift; $a =~ s/"",""/,/g; return $a;
s/([.*?])/ x($1) /eg ' y
no removal here: [...],"","",[...]
double removal here: [ , , ]
[""remove,here""],""not"",""here"",[""also,here""]


(.*? is a non-greedy match, it stops as soon as it can, that is, at the first ] in this case.)






share|improve this answer





























    up vote
    0
    down vote













    sed '/"Male/s/"",""/,/1' filename


    Output:



    "01","5","Male","[""No.**,**kg""]","","",""
    "02","6","FeMale","[""No.""]","","",""





    share|improve this answer






















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412849%2freplace-data-between-square-brackets-and-ignore-other-occurences%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      sed '/[/,/]/s/"",""/,/'


      This would look for a line with a [, then a line with a ], and between those lines, replace the first "","" encountered on each line. It doesn't really look at where the [..] are within a line.




      As a zero-order try, something like this:



      $ cat x
      "01","5","Male","[""No.**"",""**kg""]","","",""
      $ sed 's/([.*)"",""(.*])/1,2/g' x
      "01","5","Male","[""No.**,**kg""]","","",""


      The pattern matches a [, anything, "","", anything and a ], while capturing all but the "","" so it can put the pieces back together.



      This will break for stuff like [..],"","",[..] (where the brackets close before a "","" is seen, the pattern searches for a following ]) and [.."","".."",""..] (with multiple "","" sequences inside the brackets, only one is removed).



      Slightly more genarally with Perl, though this is a horrible substitution-within-substitution trick. You should probably use a proper parser instead:



      $ cat y
      no removal here: [...],"","",[...]
      double removal here: [ "","" "","" ]
      [""remove"",""here""],""not"",""here"",[""also"",""here""]

      $ perl -pe 'sub x $a = shift; $a =~ s/"",""/,/g; return $a;
      s/([.*?])/ x($1) /eg ' y
      no removal here: [...],"","",[...]
      double removal here: [ , , ]
      [""remove,here""],""not"",""here"",[""also,here""]


      (.*? is a non-greedy match, it stops as soon as it can, that is, at the first ] in this case.)






      share|improve this answer


























        up vote
        3
        down vote



        accepted










        sed '/[/,/]/s/"",""/,/'


        This would look for a line with a [, then a line with a ], and between those lines, replace the first "","" encountered on each line. It doesn't really look at where the [..] are within a line.




        As a zero-order try, something like this:



        $ cat x
        "01","5","Male","[""No.**"",""**kg""]","","",""
        $ sed 's/([.*)"",""(.*])/1,2/g' x
        "01","5","Male","[""No.**,**kg""]","","",""


        The pattern matches a [, anything, "","", anything and a ], while capturing all but the "","" so it can put the pieces back together.



        This will break for stuff like [..],"","",[..] (where the brackets close before a "","" is seen, the pattern searches for a following ]) and [.."","".."",""..] (with multiple "","" sequences inside the brackets, only one is removed).



        Slightly more genarally with Perl, though this is a horrible substitution-within-substitution trick. You should probably use a proper parser instead:



        $ cat y
        no removal here: [...],"","",[...]
        double removal here: [ "","" "","" ]
        [""remove"",""here""],""not"",""here"",[""also"",""here""]

        $ perl -pe 'sub x $a = shift; $a =~ s/"",""/,/g; return $a;
        s/([.*?])/ x($1) /eg ' y
        no removal here: [...],"","",[...]
        double removal here: [ , , ]
        [""remove,here""],""not"",""here"",[""also,here""]


        (.*? is a non-greedy match, it stops as soon as it can, that is, at the first ] in this case.)






        share|improve this answer
























          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          sed '/[/,/]/s/"",""/,/'


          This would look for a line with a [, then a line with a ], and between those lines, replace the first "","" encountered on each line. It doesn't really look at where the [..] are within a line.




          As a zero-order try, something like this:



          $ cat x
          "01","5","Male","[""No.**"",""**kg""]","","",""
          $ sed 's/([.*)"",""(.*])/1,2/g' x
          "01","5","Male","[""No.**,**kg""]","","",""


          The pattern matches a [, anything, "","", anything and a ], while capturing all but the "","" so it can put the pieces back together.



          This will break for stuff like [..],"","",[..] (where the brackets close before a "","" is seen, the pattern searches for a following ]) and [.."","".."",""..] (with multiple "","" sequences inside the brackets, only one is removed).



          Slightly more genarally with Perl, though this is a horrible substitution-within-substitution trick. You should probably use a proper parser instead:



          $ cat y
          no removal here: [...],"","",[...]
          double removal here: [ "","" "","" ]
          [""remove"",""here""],""not"",""here"",[""also"",""here""]

          $ perl -pe 'sub x $a = shift; $a =~ s/"",""/,/g; return $a;
          s/([.*?])/ x($1) /eg ' y
          no removal here: [...],"","",[...]
          double removal here: [ , , ]
          [""remove,here""],""not"",""here"",[""also,here""]


          (.*? is a non-greedy match, it stops as soon as it can, that is, at the first ] in this case.)






          share|improve this answer














          sed '/[/,/]/s/"",""/,/'


          This would look for a line with a [, then a line with a ], and between those lines, replace the first "","" encountered on each line. It doesn't really look at where the [..] are within a line.




          As a zero-order try, something like this:



          $ cat x
          "01","5","Male","[""No.**"",""**kg""]","","",""
          $ sed 's/([.*)"",""(.*])/1,2/g' x
          "01","5","Male","[""No.**,**kg""]","","",""


          The pattern matches a [, anything, "","", anything and a ], while capturing all but the "","" so it can put the pieces back together.



          This will break for stuff like [..],"","",[..] (where the brackets close before a "","" is seen, the pattern searches for a following ]) and [.."","".."",""..] (with multiple "","" sequences inside the brackets, only one is removed).



          Slightly more genarally with Perl, though this is a horrible substitution-within-substitution trick. You should probably use a proper parser instead:



          $ cat y
          no removal here: [...],"","",[...]
          double removal here: [ "","" "","" ]
          [""remove"",""here""],""not"",""here"",[""also"",""here""]

          $ perl -pe 'sub x $a = shift; $a =~ s/"",""/,/g; return $a;
          s/([.*?])/ x($1) /eg ' y
          no removal here: [...],"","",[...]
          double removal here: [ , , ]
          [""remove,here""],""not"",""here"",[""also,here""]


          (.*? is a non-greedy match, it stops as soon as it can, that is, at the first ] in this case.)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 24 '17 at 19:18

























          answered Dec 24 '17 at 18:57









          ilkkachu

          49.9k674137




          49.9k674137






















              up vote
              0
              down vote













              sed '/"Male/s/"",""/,/1' filename


              Output:



              "01","5","Male","[""No.**,**kg""]","","",""
              "02","6","FeMale","[""No.""]","","",""





              share|improve this answer


























                up vote
                0
                down vote













                sed '/"Male/s/"",""/,/1' filename


                Output:



                "01","5","Male","[""No.**,**kg""]","","",""
                "02","6","FeMale","[""No.""]","","",""





                share|improve this answer
























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  sed '/"Male/s/"",""/,/1' filename


                  Output:



                  "01","5","Male","[""No.**,**kg""]","","",""
                  "02","6","FeMale","[""No.""]","","",""





                  share|improve this answer














                  sed '/"Male/s/"",""/,/1' filename


                  Output:



                  "01","5","Male","[""No.**,**kg""]","","",""
                  "02","6","FeMale","[""No.""]","","",""






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 5 at 12:18









                  grg

                  1857




                  1857










                  answered Dec 25 '17 at 5:25









                  Praveen Kumar BS

                  1,010128




                  1,010128






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412849%2freplace-data-between-square-brackets-and-ignore-other-occurences%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay