Replace data between Square Brackets and ignore other occurences
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have a csv file with below data
Input
"01","5","Male","[""No.**"",""**kg""]","","",""
"02","6","FeMale","[""No.""]","","",""
I would like to replace the "," string which is between the square brackets but not the data outside the square brackets.
Output:
"01","5","Male","[""No.**,**kg""]","","",""
"02","6","FeMale","[""No.""]","","",""
I tried below command, but it does replace data outside the square brackets, but not for all lines.
sed '/[/,/]/s/"",""/,/'
text-processing
add a comment |Â
up vote
1
down vote
favorite
I have a csv file with below data
Input
"01","5","Male","[""No.**"",""**kg""]","","",""
"02","6","FeMale","[""No.""]","","",""
I would like to replace the "," string which is between the square brackets but not the data outside the square brackets.
Output:
"01","5","Male","[""No.**,**kg""]","","",""
"02","6","FeMale","[""No.""]","","",""
I tried below command, but it does replace data outside the square brackets, but not for all lines.
sed '/[/,/]/s/"",""/,/'
text-processing
seems to me it's Python dictionary output ?
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 24 '17 at 19:08
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a csv file with below data
Input
"01","5","Male","[""No.**"",""**kg""]","","",""
"02","6","FeMale","[""No.""]","","",""
I would like to replace the "," string which is between the square brackets but not the data outside the square brackets.
Output:
"01","5","Male","[""No.**,**kg""]","","",""
"02","6","FeMale","[""No.""]","","",""
I tried below command, but it does replace data outside the square brackets, but not for all lines.
sed '/[/,/]/s/"",""/,/'
text-processing
I have a csv file with below data
Input
"01","5","Male","[""No.**"",""**kg""]","","",""
"02","6","FeMale","[""No.""]","","",""
I would like to replace the "," string which is between the square brackets but not the data outside the square brackets.
Output:
"01","5","Male","[""No.**,**kg""]","","",""
"02","6","FeMale","[""No.""]","","",""
I tried below command, but it does replace data outside the square brackets, but not for all lines.
sed '/[/,/]/s/"",""/,/'
text-processing
asked Dec 24 '17 at 18:49
Harish
335
335
seems to me it's Python dictionary output ?
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 24 '17 at 19:08
add a comment |Â
seems to me it's Python dictionary output ?
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 24 '17 at 19:08
seems to me it's Python dictionary output ?
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 24 '17 at 19:08
seems to me it's Python dictionary output ?
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 24 '17 at 19:08
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
sed '/[/,/]/s/"",""/,/'
This would look for a line with a [
, then a line with a ]
, and between those lines, replace the first "",""
encountered on each line. It doesn't really look at where the [..]
are within a line.
As a zero-order try, something like this:
$ cat x
"01","5","Male","[""No.**"",""**kg""]","","",""
$ sed 's/([.*)"",""(.*])/1,2/g' x
"01","5","Male","[""No.**,**kg""]","","",""
The pattern matches a [
, anything, "",""
, anything and a ]
, while capturing all but the "",""
so it can put the pieces back together.
This will break for stuff like [..],"","",[..]
(where the brackets close before a "",""
is seen, the pattern searches for a following ]
) and [.."","".."",""..]
(with multiple "",""
sequences inside the brackets, only one is removed).
Slightly more genarally with Perl, though this is a horrible substitution-within-substitution trick. You should probably use a proper parser instead:
$ cat y
no removal here: [...],"","",[...]
double removal here: [ "","" "","" ]
[""remove"",""here""],""not"",""here"",[""also"",""here""]
$ perl -pe 'sub x $a = shift; $a =~ s/"",""/,/g; return $a;
s/([.*?])/ x($1) /eg ' y
no removal here: [...],"","",[...]
double removal here: [ , , ]
[""remove,here""],""not"",""here"",[""also,here""]
(.*?
is a non-greedy match, it stops as soon as it can, that is, at the first ]
in this case.)
add a comment |Â
up vote
0
down vote
sed '/"Male/s/"",""/,/1' filename
Output:
"01","5","Male","[""No.**,**kg""]","","",""
"02","6","FeMale","[""No.""]","","",""
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
sed '/[/,/]/s/"",""/,/'
This would look for a line with a [
, then a line with a ]
, and between those lines, replace the first "",""
encountered on each line. It doesn't really look at where the [..]
are within a line.
As a zero-order try, something like this:
$ cat x
"01","5","Male","[""No.**"",""**kg""]","","",""
$ sed 's/([.*)"",""(.*])/1,2/g' x
"01","5","Male","[""No.**,**kg""]","","",""
The pattern matches a [
, anything, "",""
, anything and a ]
, while capturing all but the "",""
so it can put the pieces back together.
This will break for stuff like [..],"","",[..]
(where the brackets close before a "",""
is seen, the pattern searches for a following ]
) and [.."","".."",""..]
(with multiple "",""
sequences inside the brackets, only one is removed).
Slightly more genarally with Perl, though this is a horrible substitution-within-substitution trick. You should probably use a proper parser instead:
$ cat y
no removal here: [...],"","",[...]
double removal here: [ "","" "","" ]
[""remove"",""here""],""not"",""here"",[""also"",""here""]
$ perl -pe 'sub x $a = shift; $a =~ s/"",""/,/g; return $a;
s/([.*?])/ x($1) /eg ' y
no removal here: [...],"","",[...]
double removal here: [ , , ]
[""remove,here""],""not"",""here"",[""also,here""]
(.*?
is a non-greedy match, it stops as soon as it can, that is, at the first ]
in this case.)
add a comment |Â
up vote
3
down vote
accepted
sed '/[/,/]/s/"",""/,/'
This would look for a line with a [
, then a line with a ]
, and between those lines, replace the first "",""
encountered on each line. It doesn't really look at where the [..]
are within a line.
As a zero-order try, something like this:
$ cat x
"01","5","Male","[""No.**"",""**kg""]","","",""
$ sed 's/([.*)"",""(.*])/1,2/g' x
"01","5","Male","[""No.**,**kg""]","","",""
The pattern matches a [
, anything, "",""
, anything and a ]
, while capturing all but the "",""
so it can put the pieces back together.
This will break for stuff like [..],"","",[..]
(where the brackets close before a "",""
is seen, the pattern searches for a following ]
) and [.."","".."",""..]
(with multiple "",""
sequences inside the brackets, only one is removed).
Slightly more genarally with Perl, though this is a horrible substitution-within-substitution trick. You should probably use a proper parser instead:
$ cat y
no removal here: [...],"","",[...]
double removal here: [ "","" "","" ]
[""remove"",""here""],""not"",""here"",[""also"",""here""]
$ perl -pe 'sub x $a = shift; $a =~ s/"",""/,/g; return $a;
s/([.*?])/ x($1) /eg ' y
no removal here: [...],"","",[...]
double removal here: [ , , ]
[""remove,here""],""not"",""here"",[""also,here""]
(.*?
is a non-greedy match, it stops as soon as it can, that is, at the first ]
in this case.)
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
sed '/[/,/]/s/"",""/,/'
This would look for a line with a [
, then a line with a ]
, and between those lines, replace the first "",""
encountered on each line. It doesn't really look at where the [..]
are within a line.
As a zero-order try, something like this:
$ cat x
"01","5","Male","[""No.**"",""**kg""]","","",""
$ sed 's/([.*)"",""(.*])/1,2/g' x
"01","5","Male","[""No.**,**kg""]","","",""
The pattern matches a [
, anything, "",""
, anything and a ]
, while capturing all but the "",""
so it can put the pieces back together.
This will break for stuff like [..],"","",[..]
(where the brackets close before a "",""
is seen, the pattern searches for a following ]
) and [.."","".."",""..]
(with multiple "",""
sequences inside the brackets, only one is removed).
Slightly more genarally with Perl, though this is a horrible substitution-within-substitution trick. You should probably use a proper parser instead:
$ cat y
no removal here: [...],"","",[...]
double removal here: [ "","" "","" ]
[""remove"",""here""],""not"",""here"",[""also"",""here""]
$ perl -pe 'sub x $a = shift; $a =~ s/"",""/,/g; return $a;
s/([.*?])/ x($1) /eg ' y
no removal here: [...],"","",[...]
double removal here: [ , , ]
[""remove,here""],""not"",""here"",[""also,here""]
(.*?
is a non-greedy match, it stops as soon as it can, that is, at the first ]
in this case.)
sed '/[/,/]/s/"",""/,/'
This would look for a line with a [
, then a line with a ]
, and between those lines, replace the first "",""
encountered on each line. It doesn't really look at where the [..]
are within a line.
As a zero-order try, something like this:
$ cat x
"01","5","Male","[""No.**"",""**kg""]","","",""
$ sed 's/([.*)"",""(.*])/1,2/g' x
"01","5","Male","[""No.**,**kg""]","","",""
The pattern matches a [
, anything, "",""
, anything and a ]
, while capturing all but the "",""
so it can put the pieces back together.
This will break for stuff like [..],"","",[..]
(where the brackets close before a "",""
is seen, the pattern searches for a following ]
) and [.."","".."",""..]
(with multiple "",""
sequences inside the brackets, only one is removed).
Slightly more genarally with Perl, though this is a horrible substitution-within-substitution trick. You should probably use a proper parser instead:
$ cat y
no removal here: [...],"","",[...]
double removal here: [ "","" "","" ]
[""remove"",""here""],""not"",""here"",[""also"",""here""]
$ perl -pe 'sub x $a = shift; $a =~ s/"",""/,/g; return $a;
s/([.*?])/ x($1) /eg ' y
no removal here: [...],"","",[...]
double removal here: [ , , ]
[""remove,here""],""not"",""here"",[""also,here""]
(.*?
is a non-greedy match, it stops as soon as it can, that is, at the first ]
in this case.)
edited Dec 24 '17 at 19:18
answered Dec 24 '17 at 18:57
ilkkachu
49.9k674137
49.9k674137
add a comment |Â
add a comment |Â
up vote
0
down vote
sed '/"Male/s/"",""/,/1' filename
Output:
"01","5","Male","[""No.**,**kg""]","","",""
"02","6","FeMale","[""No.""]","","",""
add a comment |Â
up vote
0
down vote
sed '/"Male/s/"",""/,/1' filename
Output:
"01","5","Male","[""No.**,**kg""]","","",""
"02","6","FeMale","[""No.""]","","",""
add a comment |Â
up vote
0
down vote
up vote
0
down vote
sed '/"Male/s/"",""/,/1' filename
Output:
"01","5","Male","[""No.**,**kg""]","","",""
"02","6","FeMale","[""No.""]","","",""
sed '/"Male/s/"",""/,/1' filename
Output:
"01","5","Male","[""No.**,**kg""]","","",""
"02","6","FeMale","[""No.""]","","",""
edited Jan 5 at 12:18
grg
1857
1857
answered Dec 25 '17 at 5:25
Praveen Kumar BS
1,010128
1,010128
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412849%2freplace-data-between-square-brackets-and-ignore-other-occurences%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
seems to me it's Python dictionary output ?
â Ã±ÃÂñýàñüÃÂÃÂùcñ÷
Dec 24 '17 at 19:08