Awk/Sed/Tr Replace non alpha-numeric with hex value

All off topic discussions go here. Everything from the funny thing your cat did to your favorite tv shows. Non-programming computer questions are ok too.
Post Reply
User avatar
OdinVex
Member
Member
Posts: 55
Joined: Tue Sep 07, 2010 11:00 pm

Awk/Sed/Tr Replace non alpha-numeric with hex value

Post by OdinVex »

I've got a byte string and need to search and replace all characters not in the range A-Za-z0-9 to it's hexadecimal equivalent prepended by &#x and appended with ; preferably with the hexadecimal in upper-case. I can use Awk, Sed, and Tr. Any Awk/Sed/Tr vets out there? :/ Greatly appreciate it.
“...No rest, no peace...” ― Odin Vex
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Awk/Sed/Tr Replace non alpha-numeric with hex value

Post by JamesM »

Can you not use perl -e ?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Awk/Sed/Tr Replace non alpha-numeric with hex value

Post by Combuster »

od is the appropriate tool for converting byte data into hex strings, and a simple pattern substitution on top of that can arrange for the corresponding html encoding. This does mean that even your alphanumerics will be encoded by default which might not necessarily be a problem for starters. It's part of coreutils on linux and as such just as readily available as tr.

The problem is that substitutions on their own can't perform other functions, and the awk manual lacks a function to convert between character ordinal and string, leaving you with the mindless expand-all-cases scenario without using that.

Of course, the task is sufficiently simple that just building a C app is quicker and more performing compared to having awk invoke a numerical conversion app for each character.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply