Deepak
Deepak

Reputation: 417

Extract text between two special characters

Trying to extract the text between the special characters "\ and \" through sed

Ex: "\hell@#$\"},

expected output : hell@#$

Upvotes: 0

Views: 1617

Answers (2)

potong
potong

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

David C. Rankin
David C. Rankin

Reputation: 84599

You can do it quite easily with using a capture-group and backreference with basic regular-expressions:

sed 's/^["][\]\([^\]*\).*$/\1/'

Explanation

  • Normal substitution sed 's/find/replace/, where
  • find is ^["][\] 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;
  • replace is \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

Related Questions