Replace "[" with [using thirst

1

I'm trying to correct some formatting errors in a file and I have as input:

"[""teste""]"

And I wanted to get an output like this:

["teste"]

I already tried this command but it gives me error:

sed -i s/"["/[/g *.csv
sed -i s/"]"/]/g *.csv

The error displayed is as follows:

sed: -e expressão #1, caractere 7: Comando 's' inacabado (s/// - faltou delimitador)

Can someone help me?

    
asked by anonymous 25.10.2017 / 18:59

2 answers

2

It's full of problems in your line of code.

  • The argument passed to is being interpreted by , which can result in unexpected effects
  • [ is the list metacharacter (or linked list if used in conjunction with ^ , and it is not escaped
  • Same for ]
  • Correction

    sed 's/"\([][]\)"//g'
    

    Explaining:

  • The argument is protected against any interpretation of the tag as it is between apostrophes
  • [][] is the list that includes the characters [ and ] ; this is due to a special syntax of lists, ] can be placed as the first character of the list that will be interpreted like this, not as list closing, then []a] would be the list containing ] and a
  • \( is indicating the presence of a group; the traditional does not interpret ( as a group metacharacter; could also have linked the expanded interpretation of regular expressions, but I can not remember if it is -e or -E
  • \) is the group closing
  • is the mirror, use what was found in the group 1 , since we only have a single group, and this group is composed of the [][] list, this means that it is the [ character or the character ]
  • See working at:

    DESKTOP-NLIG01H+Jefferson Quesado@DESKTOP-NLIG01H MINGW32 ~
    $ echo '"[""teste""]"' | sed 's/"\([][]\)"//g'
    ["teste"]
    

    Print to prove point:

        
    25.10.2017 / 19:21
    0

    Test ai

    sed -e 's/^"\["/[/' -e 's/"]"$/]/' *.csv
    
        
    25.10.2017 / 19:27