regex to add missing quotes
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
I am trying to add missing quotes at the ends of some lines in a text file.
I find that the regex [^"]$ suffices to find lines with missing terminal doublequotes and so tried the following replacement using a backreference (which tbh I've never used before). Using parens around the 'capture group' I hoped that sed would allow backreference to that group, but
sed 's|([^"]$)|1"|g' bigfile.tsv
hits
sed: -e expression #1, char 17: invalid reference 1 on `s' command's RHS
and same if I don't escape the replacement quotes
sed 's|([^"]$)|1"|g' bigfile.tsv
(tho now its char 16 that's offensive) . How does the backreference go? https://xkcd.com/1171/
sed regular-expression
add a comment |
up vote
3
down vote
favorite
I am trying to add missing quotes at the ends of some lines in a text file.
I find that the regex [^"]$ suffices to find lines with missing terminal doublequotes and so tried the following replacement using a backreference (which tbh I've never used before). Using parens around the 'capture group' I hoped that sed would allow backreference to that group, but
sed 's|([^"]$)|1"|g' bigfile.tsv
hits
sed: -e expression #1, char 17: invalid reference 1 on `s' command's RHS
and same if I don't escape the replacement quotes
sed 's|([^"]$)|1"|g' bigfile.tsv
(tho now its char 16 that's offensive) . How does the backreference go? https://xkcd.com/1171/
sed regular-expression
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I am trying to add missing quotes at the ends of some lines in a text file.
I find that the regex [^"]$ suffices to find lines with missing terminal doublequotes and so tried the following replacement using a backreference (which tbh I've never used before). Using parens around the 'capture group' I hoped that sed would allow backreference to that group, but
sed 's|([^"]$)|1"|g' bigfile.tsv
hits
sed: -e expression #1, char 17: invalid reference 1 on `s' command's RHS
and same if I don't escape the replacement quotes
sed 's|([^"]$)|1"|g' bigfile.tsv
(tho now its char 16 that's offensive) . How does the backreference go? https://xkcd.com/1171/
sed regular-expression
I am trying to add missing quotes at the ends of some lines in a text file.
I find that the regex [^"]$ suffices to find lines with missing terminal doublequotes and so tried the following replacement using a backreference (which tbh I've never used before). Using parens around the 'capture group' I hoped that sed would allow backreference to that group, but
sed 's|([^"]$)|1"|g' bigfile.tsv
hits
sed: -e expression #1, char 17: invalid reference 1 on `s' command's RHS
and same if I don't escape the replacement quotes
sed 's|([^"]$)|1"|g' bigfile.tsv
(tho now its char 16 that's offensive) . How does the backreference go? https://xkcd.com/1171/
sed regular-expression
sed regular-expression
edited 13 hours ago
asked 14 hours ago


jeremy_rutman
14511
14511
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
5
down vote
When you run sed
without -E
, then the expression is a basic regular expression and the capture groups must be written as (...)
. When you use -E
to enable extended regular expressions, capture groups are written (...)
.
The inside
[...]
is literal, so your expression would also avoid adding a double quote on lines ending with . Some of the other escaping is also unnecessary.
Therefore, you may write your sed
command as
sed 's/([^"])$/1"/'
or as
sed -E 's/([^"])$/1"/'
Or, using &
:
sed 's/[^"]$/&"/'
The &
in the replacement part of the expression will be substituted by the part of the input that matched the regular expression.
A couple of other alternatives that does not use a capture group:
sed '/[^"]$/ s/$/"/'
This applies s/$/"/
to all lines that matches /[^"]$/
.
Or, alternatively,
sed '/"$/ !s/$/"/'
This applies s/$/"/
to all lines that don't match /"$/
(there's a slight difference from the other approaches here in that it also adds a "
to empty lines).
Note that in all cases, the g
flag at the end is definitely not needed.
add a comment |
up vote
2
down vote
Try sed -e 's|([^"]$)|1"|g' bigfile.tsv
.
New contributor
U. Windl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
thank you sir, that did the trick.
– jeremy_rutman
13 hours ago
This still does not add the missing"
to lines ending with backslash and it needlessly usesg
at the end (an anchored expression can only match once).
– Kusalananda
13 hours ago
I just provided a hint about the error message; I did not intend to solve the whole problem for the author. Mostly because I think the author will learn more when doing it himself or herself.
– U. Windl
13 hours ago
@U.Windl They may, or they may not ever notice.
– Kusalananda
13 hours ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
When you run sed
without -E
, then the expression is a basic regular expression and the capture groups must be written as (...)
. When you use -E
to enable extended regular expressions, capture groups are written (...)
.
The inside
[...]
is literal, so your expression would also avoid adding a double quote on lines ending with . Some of the other escaping is also unnecessary.
Therefore, you may write your sed
command as
sed 's/([^"])$/1"/'
or as
sed -E 's/([^"])$/1"/'
Or, using &
:
sed 's/[^"]$/&"/'
The &
in the replacement part of the expression will be substituted by the part of the input that matched the regular expression.
A couple of other alternatives that does not use a capture group:
sed '/[^"]$/ s/$/"/'
This applies s/$/"/
to all lines that matches /[^"]$/
.
Or, alternatively,
sed '/"$/ !s/$/"/'
This applies s/$/"/
to all lines that don't match /"$/
(there's a slight difference from the other approaches here in that it also adds a "
to empty lines).
Note that in all cases, the g
flag at the end is definitely not needed.
add a comment |
up vote
5
down vote
When you run sed
without -E
, then the expression is a basic regular expression and the capture groups must be written as (...)
. When you use -E
to enable extended regular expressions, capture groups are written (...)
.
The inside
[...]
is literal, so your expression would also avoid adding a double quote on lines ending with . Some of the other escaping is also unnecessary.
Therefore, you may write your sed
command as
sed 's/([^"])$/1"/'
or as
sed -E 's/([^"])$/1"/'
Or, using &
:
sed 's/[^"]$/&"/'
The &
in the replacement part of the expression will be substituted by the part of the input that matched the regular expression.
A couple of other alternatives that does not use a capture group:
sed '/[^"]$/ s/$/"/'
This applies s/$/"/
to all lines that matches /[^"]$/
.
Or, alternatively,
sed '/"$/ !s/$/"/'
This applies s/$/"/
to all lines that don't match /"$/
(there's a slight difference from the other approaches here in that it also adds a "
to empty lines).
Note that in all cases, the g
flag at the end is definitely not needed.
add a comment |
up vote
5
down vote
up vote
5
down vote
When you run sed
without -E
, then the expression is a basic regular expression and the capture groups must be written as (...)
. When you use -E
to enable extended regular expressions, capture groups are written (...)
.
The inside
[...]
is literal, so your expression would also avoid adding a double quote on lines ending with . Some of the other escaping is also unnecessary.
Therefore, you may write your sed
command as
sed 's/([^"])$/1"/'
or as
sed -E 's/([^"])$/1"/'
Or, using &
:
sed 's/[^"]$/&"/'
The &
in the replacement part of the expression will be substituted by the part of the input that matched the regular expression.
A couple of other alternatives that does not use a capture group:
sed '/[^"]$/ s/$/"/'
This applies s/$/"/
to all lines that matches /[^"]$/
.
Or, alternatively,
sed '/"$/ !s/$/"/'
This applies s/$/"/
to all lines that don't match /"$/
(there's a slight difference from the other approaches here in that it also adds a "
to empty lines).
Note that in all cases, the g
flag at the end is definitely not needed.
When you run sed
without -E
, then the expression is a basic regular expression and the capture groups must be written as (...)
. When you use -E
to enable extended regular expressions, capture groups are written (...)
.
The inside
[...]
is literal, so your expression would also avoid adding a double quote on lines ending with . Some of the other escaping is also unnecessary.
Therefore, you may write your sed
command as
sed 's/([^"])$/1"/'
or as
sed -E 's/([^"])$/1"/'
Or, using &
:
sed 's/[^"]$/&"/'
The &
in the replacement part of the expression will be substituted by the part of the input that matched the regular expression.
A couple of other alternatives that does not use a capture group:
sed '/[^"]$/ s/$/"/'
This applies s/$/"/
to all lines that matches /[^"]$/
.
Or, alternatively,
sed '/"$/ !s/$/"/'
This applies s/$/"/
to all lines that don't match /"$/
(there's a slight difference from the other approaches here in that it also adds a "
to empty lines).
Note that in all cases, the g
flag at the end is definitely not needed.
edited 11 hours ago


Stéphane Chazelas
292k54547887
292k54547887
answered 13 hours ago


Kusalananda
114k15218349
114k15218349
add a comment |
add a comment |
up vote
2
down vote
Try sed -e 's|([^"]$)|1"|g' bigfile.tsv
.
New contributor
U. Windl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
thank you sir, that did the trick.
– jeremy_rutman
13 hours ago
This still does not add the missing"
to lines ending with backslash and it needlessly usesg
at the end (an anchored expression can only match once).
– Kusalananda
13 hours ago
I just provided a hint about the error message; I did not intend to solve the whole problem for the author. Mostly because I think the author will learn more when doing it himself or herself.
– U. Windl
13 hours ago
@U.Windl They may, or they may not ever notice.
– Kusalananda
13 hours ago
add a comment |
up vote
2
down vote
Try sed -e 's|([^"]$)|1"|g' bigfile.tsv
.
New contributor
U. Windl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
thank you sir, that did the trick.
– jeremy_rutman
13 hours ago
This still does not add the missing"
to lines ending with backslash and it needlessly usesg
at the end (an anchored expression can only match once).
– Kusalananda
13 hours ago
I just provided a hint about the error message; I did not intend to solve the whole problem for the author. Mostly because I think the author will learn more when doing it himself or herself.
– U. Windl
13 hours ago
@U.Windl They may, or they may not ever notice.
– Kusalananda
13 hours ago
add a comment |
up vote
2
down vote
up vote
2
down vote
Try sed -e 's|([^"]$)|1"|g' bigfile.tsv
.
New contributor
U. Windl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Try sed -e 's|([^"]$)|1"|g' bigfile.tsv
.
New contributor
U. Windl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
U. Windl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 13 hours ago
U. Windl
1291
1291
New contributor
U. Windl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
U. Windl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
U. Windl is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
thank you sir, that did the trick.
– jeremy_rutman
13 hours ago
This still does not add the missing"
to lines ending with backslash and it needlessly usesg
at the end (an anchored expression can only match once).
– Kusalananda
13 hours ago
I just provided a hint about the error message; I did not intend to solve the whole problem for the author. Mostly because I think the author will learn more when doing it himself or herself.
– U. Windl
13 hours ago
@U.Windl They may, or they may not ever notice.
– Kusalananda
13 hours ago
add a comment |
1
thank you sir, that did the trick.
– jeremy_rutman
13 hours ago
This still does not add the missing"
to lines ending with backslash and it needlessly usesg
at the end (an anchored expression can only match once).
– Kusalananda
13 hours ago
I just provided a hint about the error message; I did not intend to solve the whole problem for the author. Mostly because I think the author will learn more when doing it himself or herself.
– U. Windl
13 hours ago
@U.Windl They may, or they may not ever notice.
– Kusalananda
13 hours ago
1
1
thank you sir, that did the trick.
– jeremy_rutman
13 hours ago
thank you sir, that did the trick.
– jeremy_rutman
13 hours ago
This still does not add the missing
"
to lines ending with backslash and it needlessly uses g
at the end (an anchored expression can only match once).– Kusalananda
13 hours ago
This still does not add the missing
"
to lines ending with backslash and it needlessly uses g
at the end (an anchored expression can only match once).– Kusalananda
13 hours ago
I just provided a hint about the error message; I did not intend to solve the whole problem for the author. Mostly because I think the author will learn more when doing it himself or herself.
– U. Windl
13 hours ago
I just provided a hint about the error message; I did not intend to solve the whole problem for the author. Mostly because I think the author will learn more when doing it himself or herself.
– U. Windl
13 hours ago
@U.Windl They may, or they may not ever notice.
– Kusalananda
13 hours ago
@U.Windl They may, or they may not ever notice.
– Kusalananda
13 hours ago
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%2f481273%2fregex-to-add-missing-quotes%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