Reputation: 417
Trying to extract the text between the special characters "\
and \"
through sed
Ex: "\hell@#$\"},
expected output : hell@#$
Upvotes: 0
Views: 1617
Reputation: 58488
This might work for you (GNU sed):
sed -nE '/"\\[^\\]*\\+([^\\"][^\\]*\\+)*"/{s/"\\/\n/;s/.*\n//;s/\\"/\n/;P;D}' file
The solution comes in two parts:
Firstly, a regexp to determine whether a pair of two characters exists. This can be tricky as a negated class is insufficient because edge cases can easily defeat a simplistic approach.
Secondly, once a pair of characters does exist the text between them must be extracted piece meal.
Upvotes: 0
Reputation: 84599
You can do it quite easily with using a capture-group and backreference with basic regular-expressions:
sed 's/^["][\]\([^\]*\).*$/\1/'
Explanation
sed 's/find/replace/
, where^["][\]
a double-quote and \
before beginning the capture \(...\)
which contains [^\]*
(zero or more characters not a \
), the closing of the capture \)
and then .*$
the remainder of the string;\1
(the first backreference) containing the text captured between \(...\)
.(note: if your "\
doesn't begin the string, remove the first '^'
anchor)
Example
$ echo '"\hell@#$\"},' | sed 's/^["][\]\([^\]*\).*$/\1/'
hell@#$
Look things over and let me know if you have questions.
Upvotes: 2