-7

This is the source string:

%5B++The+transmission+is+%5B150mhz%5D+The+year+is+%282017%29+This+is+%2A+great+%2A+so+far++%5D
  • Is it possible to make a pattern only with GNU SED to:
    1. Replace a singe + to a single space
    2. From %**abc to "\x**"abc (the first two characters after the % is always hex UTF-8)
    3. Every sentence must have one " at the beginning and one " at end of the sentence

So the result to be like this:

"\x5B"  "The" "transmission" "is" "\x5B"150mhz"\x5D" "The" "year" "is" "\x28"2017"\x29" "This" "is" "\x2A" "great" "\x2A" "so" "far"  "\x5D"

So when echo is used with the string:

echo -e "\x5B"  "The" "transmission" "is" "\x5B"150mhz"\x5D" "The" "year" "is" "\x28"2017"\x29" "This" "is" "\x2A" "great" "\x2A" "so" "far"  "\x5D"

Will result exactly like this:

[ The transmission is [150mhz] The year is (2017) This is * great * so far ]

1 Answers1

4

This works:

sed -r -e 's/(.*)/"\1"/' -e 's/\+/" "/g' -e 's/""/ /g' -e 's/\%/\\x/g' -e 's/("\\x.{2})/\1"/g' -e 's/""\s+/" /g' -e 's/"(.*)"/"\1/' -e 's/([^"]|(([0-9]|[a-z])))(\\x[0-9]([a-zA-Z]|[0-9]))" /\1"\4" /g' src.txt

Result:

"\x5B"  "The" "transmission" "is" "\x5B"150mhz"\x5D" "The" "year" "is" "\x28"2017"\x29" "This" "is" "\x2A" "great" "\x2A" "so" "far"  "\x5D"

Then on:

echo -e "\x5B"  "The" "transmission" "is" "\x5B"150mhz"\x5D" "The" "year" "is" "\x28"2017"\x29" "This" "is" "\x2A" "great" "\x2A" "so" "far"  "\x5D"

Result:

[ The transmission is [150mhz] The year is (2017) This is * great * so far ]

I don't think the sed is the best tool to use here but since your looking to learn .

George Udosen
  • 37,534