[SOLVED] Replace character with hex value of my choosing from shell cmd line? (Solaris 5, ksh)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Replace character with hex value of my choosing from shell cmd line? (Solaris 5, ksh)
Hi everyone. I have a rather odd task I am trying to accomplish.
I am bouncing a file across platforms (windows->solaris->mainframe), and the file is starting out with a "special" character (the registered trademark "circle R") in some of the records. This character is not in the EBCDIC character set on the MF, so it is unrecognizable.
The MF developer I am working with asked if it is possible to replace the character with a specific hex value (AF) before it gets to the MF.
I was putzing around with sed, tr, etc. on the ksh command line, hoping to find an easy way to get one of them to substitute hex instead of ASCII. I have found that the usual shell utilities recognize the trademark character, so homing in on what to replace is solved. But I cannot get anything to actually substitute in the hex sequence I want. E.g. I was thinking something like...
>cat special_file | sed 's/R/\0xAF/g'
But my version of sed does not seem to have hex "editing" capability.
You might try using iconv instead. This is just the kind of situation it was designed for.
Code:
iconv -f UTF-8 -t EBCDIC-US//TRANSLIT file
The TRANSLIT option means it will attempt to replace unsupported characters with supported substitutes. There's also an IGNORE option, which will simply drop them instead.
Thanks to David and jschiwal for the real replies. It appears that my installation of Solaris does not have the EBCDIC character set for iconv. MY /usr/lib/iconv/iconv_data file is as follows:
-----------------------------------------------------------------------------
646 8859 8859.646 8859.646.t
646de 8859 646de.8859 646de.8859.t
646da 8859 646da.8859 646da.8859.t
646en 8859 646en.8859 646en.8859.t
646fr 8859 646fr.8859 646fr.8859.t
646it 8859 646it.8859 646it.8859.t
646es 8859 646es.8859 646es.8859.t
646sv 8859 646sv.8859 646sv.8859.t
8859 646 8859.646 8859.646.t
8859 646de 8859.646de 8859.646de.t
8859 646da 8859.646da 8859.646da.t
8859 646en 8859.646en 8859.646en.t
8859 646fr 8859.646fr 8859.646fr.t
8859 646it 8859.646it 8859.646it.t
8859 646es 8859.646es 8859.646es.t
8859 646sv 8859.646sv 8859.646sv.t
----------------------------------------------------------------------------------
So I could not get iconv to work. And you are correct, sed does not need \0x, it just needs \x (I just typed that command off the top of my head). Unfortunately, it appears that the variant of sed on our system does not have hex substitution in the first place.
I did manage to get tr to work, at least on the Solaris side. After running the file through tr and looking at a hex dump (via od) of the result, it appears that the desired hex value was substituted in (if one uses the correct octal equivalent in the tr command). But when I transfer the file to the mainframe and look at the raw hex there, it seems that the file is being modified by the transfer. I guess if I'm ftp'ing in ASCII mode, there's no reason to expect the non-printable characters to make it through intact.
Anyway, thanks again for the real replies. The experimentation continues...
Last edited by discomurder; 06-10-2011 at 11:40 AM.
Looking in the sed info file, I see it is a GNU extension.
I also looked in the 1p manpage for the `tr' command. Then I was just lookin at the posix man page. You should be able to translate the character if you use octal codes. 0xAF = \0257.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.