Using sed to split hex string

Hi,

I'm looking to split the following hex string into rows of four elements.

I've tried the following but it doesn't seem to work. How can I tell sed to match based on a pair of number(s) and letter(s), and add a newline every 4 pairs?
In addition, I need to add another newline after every string has been processed.

echo "1C 50 A9 D3 8B B0 24 9C 14 1C 50 A9 D3 8B B0 24" | sed -nr 's/[0-9A-Z]{2}/\n/4p'
1C 50 A9
 8B B0 24 9C 14 1C 50 A9 D3 8B B0 24 9C 14

Expected output:

1C 50 A9 D3
8B B0 24 9C

14 1C 50 A9
D3 8B B0 24 
 | sed 's/\(\([0-9A-F][0-9A-F] \)\{4\}\)/\1\n/g'

Insert a \n every 12 characters:

sed -nr 's/.{12}/&\n/gp'

If you really want to indentify 4 elements:

sed -nr 's/([0-9A-F]{2} ){4}/&\n/gp'

With BRE (no -r) it is like the previous solution:

sed -n 's/\([0-9A-F][0-9A-F] \)\{4\}/&\n/gp'
1 Like

regex / wctype offer a "character class" for hex:

sed -nr 's/([[:xdigit:]]{2} ){4}/&\n/gp'
1 Like

Hi.

echo "1C 50 A9 D3 8B B0 24 9C 14 1C 50 A9 D3 8B B0 24" |
fold -12 |
sed '2~2a\
'

producing:

1C 50 A9 D3 
8B B0 24 9C 

14 1C 50 A9 
D3 8B B0 24

Where the GNU extension '2~2' starts with line 2, then after each 2nd line adds a newline.

On a system like:

OS, ker|rel, machine: Linux, 3.16.0-7-amd64, x86_64
Distribution        : Debian 8.11 (jessie) 
bash GNU bash 4.3.30
fold (GNU coreutils) 8.23
sed (GNU sed) 4.2.2

Best wishes ... cheers, drl

You might try as well

echo "1C 50 A9 D3 8B B0 24 9C 14 1C 50 A9 D3 8B B0 24" | tr ' ' $'\n' | paste -sd'   \n'