Converting parts of a string to "Hex"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting parts of a string to "Hex"
# 1  
Old 09-12-2012
Converting parts of a string to "Hex"

Hi Guys,

writing a small shell script, i need to convert parts of a string to "Hex". The problem is that it is not the full string that needs to be converted.
I think it's best to show an example:

$astring = "xxxxxx ABC+10+##########+DEF xxxx"

This is only an example to show how the string could look like. There can be many characters at the beginning, then there is a "starting code" ABC, a value which say's how long the element it (10 in this case), then the some unprintable hex characters (just marked as #) and the "end mark" DEF.

As the hex char's are not displayable, they should be displayed as "\x39" for example. So the above string should result in

$astring = "xxxxxx ABC+10+\x39\x55\x12\x84\xA7\x9F\x2C\xB1\xFF\x12+DEF xxxx"

after the conversion.

The only thing i can do is to loop through the string, find "ABC", get the count number and do the conversion.

Or is there a quicker and simpler solution you could think of?


Thanks a lot for your help !!!

Hans
# 2  
Old 09-12-2012
have you tried
Code:
od -x or od -h

command?
# 3  
Old 09-13-2012
od is converting a whole file. What i need is something that converts only parts of a string.
# 4  
Old 09-13-2012
Quote:
Originally Posted by HansHansen
od is converting a whole file. What i need is something that converts only parts of a string.
if od is doing what you want then extract the part of the string using sed/awk or anyother command and pass it through od and replace it back..
# 5  
Old 09-13-2012
If that string can contain arbitrary binary data, you may have problems. Many UNIX utilities (sed, awk, sh, etc ...) are designed to read text files. Text files are not expected to contain control characters and nullbytes. Nullbytes (\0) in particular could cause failure since UNIX text tools often rely on the c library's string handling functions, which use \0 as a terminator.

Regards,
Alister
# 6  
Old 09-13-2012
OK, this was a pain in the neck, but it - mayhap not too elegantly - will solve the problem given. It assumes hexdump to be installed, adapt to od if need be. You need an awk- program- file
Code:
$ cat awkfile
BEGIN{FS="+"; hd="hexdump -v -e '\"\\\\\" /1 \"X%02X\"'" }
        {for (i=1; i<=NF; i++)
                {if ($i~"ABC") {j=i+2}
                 if (i==j) {printf "%s",$i|hd;printf "%s", FS} else printf "%s%s", $i,FS}}

And then, execute that:
Code:
echo $astring|awk -f awkfile
xxxxxx ABC+10+\X39\X55\X12\X84\XA7\X9F\X2C\XB1\XFF\X12+DEF xxxx+

I know, there is an field separator too many at the end, but I'm out of patience now...
# 7  
Old 09-14-2012
Code:
perl -pe '$p=qr/ ABC\+([^+]+)\+/; /$p/; s/($p)(.{$1})/$1.join("",map{"\\x$_"}unpack("H2"x$2,$3))/e' file

Regards,
Alister

---------- Post updated at 11:02 PM ---------- Previous update was at 10:54 PM ----------

Quote:
Originally Posted by RudiC
OK, this was a pain in the neck ... <snip> ... but I'm out of patience now...
Been there myself many times. Unfortunately, your solution won't work reliably. The timing of hexdump's output and awk's output isn't in anyway guaranteed.

With a 3 line sample file and stdout a terminal, the hexdump output for all three lines shows up after all of awk's output for those three lines.

With the same sample file and stdout redirected to a file, the hexdump output for all three lines occurs in the middle of the first line of awk output.

I'm almost certain that the consistent difference is a side effect of my awk implementation increasing buffering in the absence of interactivity.

Regards,
Alister

Last edited by alister; 09-14-2012 at 08:16 AM.. Reason: Forgot space in regexp
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

Convert "hex" foldername to "ascii"

So, I have a folder, containing subdirs like this: 52334d50 52365245 524b4450 524f3350 52533950 52535050 52555550 now I want to go ahead and rename all those folder: hex -> ascii (8 Replies)
Discussion started by: pasc
8 Replies

2. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

3. UNIX for Dummies Questions & Answers

Extracting Parts of String "#" vs "%"

Hello, I have a question regarding extracting parts of a string and the meaning of # and % in the syntax. I created an example below. # filename=/first/second/third/fourth # # echo $filename /first/second/third/fourth # # echo "${filename##*/}" fourth # # echo "${filename%/*}"... (3 Replies)
Discussion started by: shah9250
3 Replies

4. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

5. Shell Programming and Scripting

tcsh - understanding difference between "echo string" and "echo string > /dev/stdout"

I came across and unexpected behavior with redirections in tcsh. I know, csh is not best for redirections, but I'd like to understand what is happening here. I have following script (called out_to_streams.csh): #!/bin/tcsh -f echo Redirected to STDOUT > /dev/stdout echo Redirected to... (2 Replies)
Discussion started by: marcink
2 Replies

6. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

7. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

8. Shell Programming and Scripting

read parts of binary files by "ranges"

i read the "cat" manpages, but i could not find to tell it like "read file XY.BIN from byte 1000 to byte 5000" can somebody please point me into the right direction? cat would be the ideal tool for my purpose, the way it behaves, but i miss this ranges option. thanks for any input. (2 Replies)
Discussion started by: scarfake
2 Replies

9. Shell Programming and Scripting

How to apply a "tolower" AWK to a few parts of a document

Hi people, i have a nice problem to solve.. in an text page i must change all the "*.php" occourences to the respective lowercase.. Example: ... <tr><td> <form action="outputEstrazione.php" method="get"> <table cellspacing='0,5' bgcolor='#000000'><tr><td> <font size='2'... (5 Replies)
Discussion started by: marconet85
5 Replies

10. Shell Programming and Scripting

input string="3MMTQSZ348GGMZRQWMJM4SD6M";output string="3MMTQ-SZ348-GGMZR-QWMJM-4SD6

input string="3MMTQSZ348GGMZRQWMJM4SD6M" output string="3MMTQ-SZ348-GGMZR-QWMJM-4SD6M" using linux shell script (4 Replies)
Discussion started by: pankajd
4 Replies
Login or Register to Ask a Question