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?
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 ].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With