0

I've a variable where I would like to extract the content surrounded by a special character. In my case the character is %.

The value stored in the variable goes this way:

"something.. : something.. %Required Value 1% something.. %Required Value 2% : something.. "

The response is filled with different special characters and the value is coming from a different script.

From the above response, I want to extract the values surrounded with %. The output I'm looking for is

Required Value 1
Required Value 2

2 Answers 2

3

Using awk:

awk -v RS='%' '(NR+1)%2' <<< "something.. : something.. %Required Value 1% something.. %Required Value 2% : something.. "
Required Value 1
Required Value 2

RS, the record separator, is set to %. The only statement prints all even record number, e.g. NR=2, 4, 6...

Sign up to request clarification or add additional context in comments.

Comments

0

Using Perl

> export X="something.. : something.. %Required Value 1% something.. %Required Value 2% : something.. "
> perl -e ' BEGIN { $p=$ENV{X}; while($p=~m/(.+?)%(.+?)%(.+)/) { $p=$3; print "$2\n" }} '
Required Value 1
Required Value 2
>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.