Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > help with sed

help with sed
Thread Tools
Fresh-Faced Recruit
Join Date: Jan 2006
Status: Offline
Reply With Quote
Jan 23, 2006, 10:29 PM
 
Hello,
I am having a problem using sed to substitute hex data in a file. I have no problem with using sed for simple substitution ie (sed s’/a/b/g’ file a > file b) (replace all a’s in file a with b’s). What I need to do is something like this (sed s’/\x20/\x30/g’ file a > file b) (where \x20 is the hex for ‘ ‘ and \x30 is the hex for ‘0’). Does anyone know the proper syntax for this?

Thanks.
     
Grizzled Veteran
Join Date: Apr 2002
Location: case.edu
Status: Offline
Reply With Quote
Jan 23, 2006, 10:52 PM
 
sed isn't really designed for working with binary data. You might have better luck with tr, which accepts the \xxx octal escapes (and maybe other types as well - check its man page).

Be sure to single-quote all your arguments so the shell doesn't eat the backslashes. You may wish to try your arguments with echo first if you aren't sure what's going on.

pb 1440x960 | 1.67, 1.5, 128, 80 | leopard
     
Junior Member
Join Date: Jun 2005
Status: Offline
Reply With Quote
Jan 24, 2006, 12:20 PM
 
You can get the list of octal, hex and decimal codes for use in the tr command by issuing a 'man ascii' in the CLI.

Walt
     
Fresh-Faced Recruit
Join Date: Jan 2006
Status: Offline
Reply With Quote
Jan 24, 2006, 08:42 PM
 
Thanks Guys,

tr worked great.
     
Grizzled Veteran
Join Date: Mar 2004
Status: Offline
Reply With Quote
Jan 25, 2006, 12:54 PM
 
Originally Posted by duneomvs
I am having a problem using sed to substitute hex data in a file. I have no problem with using sed for simple substitution ie (sed s’/a/b/g’ file a > file b) (replace all a’s in file a with b’s). What I need to do is something like this (sed s’/\x20/\x30/g’ file a > file b) (where \x20 is the hex for ‘ ‘ and \x30 is the hex for ‘0’). Does anyone know the proper syntax for this?
While the answers above apply well to 'binary' data
if instead you were referring to text inside of a text file...
then all that was needed was to "escape the escape."
Sorta like this...
Code:
hostname:~ (501) $ echo \x20 x20 hostname:~ (502) $ echo \\x20 \x20 hostname:~ (503) $ echo '\x20' \x20 hostname:~ (504) $ echo '\x20' | sed 's/\\x20/\\x30/g' \x30 hostname:~ (505) $
Maybe without slashes it's easier to see:
sed 's:\\x20:\\x30:g'
-HI-
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:25 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2