Remove "[and]" from csv file using sed

I have a line like

0,"['low', 'low', 'low']","['better', 'better', 'better', 'better', 'better', 'better']","['True', 'True', 'True', 'True', 'True', 'True', 'True']"

      

And I would like to get rid of "[] from the teaching of this line. Now I work separately

sed -i 's / \ "// g' File.csv

sed -i 's / \ [// g' File.csv

sed -i 's / \] // g' File.csv

To obtain

0, 'low', 'low', 'low', 'better', 'better', 'better', 'better', 'better', 'better', 'True', 'True', 'True', 'True', 'True', 'True', 'True'

      

Is there an efficient way to do this?

+3


source to share


2 answers


Sure.

Pasting with GNU sed

:

sed -i 's/[]"[]//g' File.csv

      

For non-GNU sed

(like the BSD version shipped with OS X) the -i

(in-place) option is not available, so you have to write to a new file and then replace:

sed 's/[]"[]//g' File.csv >NewFile.csv && mv NewFile.csv File.csv

      



In this case, you can use other commands; perhaps with the shortest command line (besides redirecting and renaming), perhaps tr

:

tr -d '[]"' <File.csv >NewFile.csv && mv NewFile.csv File.csv

      

Explanation of regex sed

:

Any set of characters within the brackets [

... ]

creates a "character class", which is shorthand for single-character interleaving. That is, the value [abc]

is a

either b

or c

. If you want to include a literal [

or ]

within a character class, you can do this by putting brackets immediately next to the other, which covers a character class: []abc]

include a

, b

, c

and ]

, and [abc[]

includes a

, b

, c

and [

. So, []"[]

includes [

, "

and ]

.

+4


source


sed -e 's/\("\[\)*\(]"\)*//g' File.csv > /tmp/File.csv && mv /tmp/File.csv File.csv

      



  • to avoid [

    , ]

    or "

    inside 'content'

    . (avoidance ]"

    "[

    should be done as well, but fewer cases for more complex sed action.
  • Posix version (sic --posix

    GNU sed)
0


source







All Articles