1

I need to parse string containing hash code value and convert the hash code is equivalent char representation. Here is the example code of it.

I see that you#39;re eligible to get ticket for show on your device#44;

now, the script should output to

I see that you're eligible to get ticket for show on your device,
5
  • Is this html encoding? Jan 21 '15 at 21:23
  • yes, it uses the html entity encoding style but without the &.
    – pratikch
    Jan 21 '15 at 21:29
  • 1
    There are tools that can do html decoding, so use sed to convert it to “proper” html encoding, then put it through one of these decoders. Jan 21 '15 at 21:34
  • How do you get from 146 to ` (grave accent, U+0060)? Or do you mean (right single quotation mark), which is at 146 in the ibm-cp1252 character set for instance? Jan 21 '15 at 22:16
  • After your edit, that still doesn't add up. HTML ’ is a control character, ` is ` (backtick, grave accent), ' (apostrophe) is ', (right single quotation mark) is ’. Jan 22 '15 at 12:46
1

perl is good for this:

$ str='I see that you#146;re eligible to get ticket for show on your device#44;'
$ perl -pe 's/#(\d+);/chr($1)/ge' <<<"$str"
I see that you’re eligible to get ticket for show on your device,

I had to set my terminal's encoding to WINDOWS-1252 to get that output. Decimal 146 is not a valid ISO-8859-1 character.


To treat those codes as HTML entities, we'll add the missing ampersand, and decode:

perl -MHTML::Entities -lne 's/(#\d+;)/&$1/g; print decode_entities($_)' <<<"$str"
5
  • Thank you for your answer, but can't use anything other than simple shell script :(
    – pratikch
    Jan 21 '15 at 21:36
  • you need to output character with code point 146, not byte 146. (This will be a 2 byte code in utf-8) Jan 21 '15 at 21:36
  • Yes I agree, this uses UTF encoding. Sorry If I've missed to mention that.
    – pratikch
    Jan 21 '15 at 21:39
  • There's a great number of charset that have RIGHT SINGLE QUOTATION MARK on 146, not only windows-1252 (all the other windows-125x as well for instance). Of course, using that character here is wrong, the OP should use the apostrophe ' instead. Jan 21 '15 at 22:07
  • agree, corrected the question.
    – pratikch
    Jan 22 '15 at 4:07
0

In bash you could do:

$ str='I see that you#39;re eligible to get ticket for show on your device#44;'
$ re='#([0-9]*);'; while [[ $str =~ $re ]]; do b="${BASH_REMATCH[1]}"; c=$(printf "$(printf '\\U%x' "$b")"); str=${str//#$b;/$c}; done; echo "$a"
I see that you're eligible to get ticket for show on your device,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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