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?
source to share
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 ]
.
source to share