Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a way to specify hex values on the tr command? 3

Status
Not open for further replies.

jbmjlm10

Programmer
Jun 16, 2004
11
US
I have a file and wanted to change any occurrences of hex 80 to hex 9f. The only way I can do this is if I specify the equivalent octal value 200 and 237, respectively. In other words, the following tr command works fine:

tr '\200' '\237' < file.in > file.out

Is there a way I can make the tr command work specifying the hex values (80 and 9f) instead?

Thank you for your help.
 
Create a function like this:
h2o(){
echo "ibase=16\nobase=8\n$1" | bc
}
And now:
tr "$(h2o 80)" "$(h2o 9F)" < file.in > file.out

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Yes, that is very clever! Thank you PCV. But did that actually work for you just the way you have it? I had to make a change as follows to make it work:

h2o(){
echo "ibase=16\nobase=8\n$1" | bc
}
tr \\"$(h2o 80)" \\"$(h2o 9F)" < file.in > file.out

The tr command still needs a \ to tell it that the value is octal at the time it is executed.

Thank you.
 
It did work for me that the way PHV had it. (I have a shell bourne).
 
incorrect, single quotes instead double ones

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top