Skip to main content
added 101 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k

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 doesn'tdon'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 doesn't match /"$/.

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.

added 49 characters in body
Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

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 doesn't match /"$/.

Note that in all cases, the g flag at the end is definitely not needed.

When you run sed without -E, then 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 doesn't match /"$/.

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 doesn't match /"$/.

Note that in all cases, the g flag at the end is definitely not needed.

added 135 characters in body
Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

When you run sed without -E, then 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 doesn't match /"$/.

Note that in all cases, the g flag at the end is definitely not needed.

When you run sed without -E, then 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/[^"]$/&"/' 

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 doesn't match /"$/.

Note that in all cases, the g flag at the end is definitely not needed.

When you run sed without -E, then 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 doesn't match /"$/.

Note that in all cases, the g flag at the end is definitely not needed.

Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k
Loading