Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove "[ and ]" from csv file using sed

Tags:

bash

sed

I have a row such as this

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

And I'd like to get rid of the "[ ] from teach of the line. Right now I am separately running

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

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

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

to get

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

Is there an efficient way of doing this?

like image 749
Swams Avatar asked May 15 '26 01:05

Swams


1 Answers

Sure.

Sticking with GNU sed:

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

For non-GNU sed (such as the BSD version that ships 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

There are other commands you can use in that case; the one with the shortest command line to do this (not counting the redirection and renaming) is probably tr:

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

Explanation of the sed regular expression:

Any set of characters inside [...] brackets creates a "character class" that is a shorthand for single-character alternation. That is, the meaning of [abc] is "a or b or c". If you want to include a literal [ or ] inside a character class, you can do so by placing the bracket immediately next to the opposite one that encloses the character class: []abc] includes a, b, c, and ], while [abc[] includes a, b, c and [. So []"[] includes [, ", and ].

like image 193
Mark Reed Avatar answered May 17 '26 15:05

Mark Reed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!