-7

This question already has an answer here:

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 ]

marked as duplicate by muru, waltinator, αғsнιη, pa4080, Videonauth Nov 25 '17 at 8:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Can't you just URL-decode the string using Python, etc.? – muru Nov 25 '17 at 4:01
  • I know only the basics of sed and i will like to learn more – GoldHaloWings Nov 25 '17 at 4:03
  • 4
    And yet you haven't demonstrated any effort at all. – muru Nov 25 '17 at 4:03
  • 3
    Then show us your attempt. – muru Nov 25 '17 at 4:09
  • 1
    I don't think you can get (2017) from your input string since it has %28 (or 0x28 left parenthesis) on both sides – steeldriver Nov 25 '17 at 4:17
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 .

  • Thank you can be something like this? echo "%2A+Hello" | sed -e 's/%/"\\/g' -e '/"\\/s/+/" /g' – GoldHaloWings Nov 25 '17 at 4:35
  • Still working on it when done will update this – George Udosen Nov 25 '17 at 4: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 5:21
  • yes will fix that! – George Udosen Nov 25 '17 at 5:22
  • : ) just telling – GoldHaloWings Nov 25 '17 at 5:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.