-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
  • 36,677
  • Thank you can be something like this? echo "%2A+Hello" | sed -e 's/%/"\/g' -e '/"\/s/+/" /g' – GoldHaloWings Nov 25 '17 at 04:35
  • Still working on it when done will update this – George Udosen Nov 25 '17 at 04:38
  • echo "%5B++The+transmission+is+%5B150mhz%5D+The+year+is+%282017%29+This+is+%2A+great+%2A+so+far++%5D" | sed -re 's/+/ /g' -e 's/%/\x/g' -e 's/\s+/" "/g' is missing two " in "\x5B150mhz\x5D" and "\x282017\x29" one " to the end – GoldHaloWings Nov 25 '17 at 05:21
  • yes will fix that! – George Udosen Nov 25 '17 at 05:22
  • : ) just telling – GoldHaloWings Nov 25 '17 at 05:29
  • @GoldHaloWings it's done see my updated answer but I feel the dup link would have been a better option, but if sed is required then my answer should help. Quite a challenge I tell you but glad I pulled through. – George Udosen Nov 25 '17 at 19:33
  • Thank very much George i appreciate your help and as i can see it was quite a challenge indeed – GoldHaloWings Nov 25 '17 at 22:27
  • sed -e 's/+/ /g' -e 's/%/\x/g' -e 's/\.../&"/g' -e 's/\/"\/g' this is more smaller with the same result sorry for the many comments – GoldHaloWings Nov 26 '17 at 02:36
  • Yes but I believe you desired the result to look like this "\x5B" "The" "transmission" "is" "\x5B"150mhz"\x5D" "The" "year" "is" "\x28"2017"\x29" "This" "is" "\x2A" "great" "\x2A" "so" "far" "\x5D". I had a similar short regex but had to modify t make the result come out as you stated in your question. I usually don't second guess the intention of the owner of the question but use what is provided to answer the question. If that is okay then go ahead and use that version! – George Udosen Nov 26 '17 at 04:40
  • Stupid of me, you are right it seems than i can't beat your pattern the best answer is still yours – GoldHaloWings Nov 26 '17 at 04:58