awk :help to parse a file to change to separated by colon ":"

Hi experts ,

I am trying to get the below output:

file :

0/6/4/1  0x0019503C2E26 5   UP    lan5 snap5     1   ETHER     Yes     224
0/6/4/0  0x0019503C2E25 6   UP    lan6 snap6     2   ETHER     Yes     224
0/2/1/0  0x0019503E6900 0   UP    lan0 snap0     3   ETHER     Yes     224
0/2/1/1  0x0019502E52AA 1   UP    lan1 snap1     4   ETHER     Yes     224

The output to be look like this: in $2 , to remove 0x00 & to separate each 2 digit with :

0/6/4/1  19:50:3C:2E:26 5   UP    lan5 snap5     1   ETHER     Yes     224
0/6/4/0  19:50:3C:2E:25 6   UP    lan6 snap6     2   ETHER     Yes     224
0/2/1/0  19:50:3E:69:00 0   UP    lan0 snap0     3   ETHER     Yes     224
0/2/1/1  19:50:2E:52:AA 1   UP    lan1 snap1     4   ETHER     Yes     224

Thanks ,

How about:

awk -F'\t' '{gsub(/^0x00/,"",$2); gsub(/../,"&:",$2);gsub(/:$/,"",$2)} 1' OFS="\t" infile

And if the input is not tab-separated but of fixed width and you need to preserve the number of spaces/tabs between the columns, then try (with some assumptions):

awk '{t2=$2;s2=index($0,t2);l2=length(t2)
sub(/^0x00/,"",t2);gsub(/../,"&:",t2);sub(/:$/,"",t2)
print substr($0,1,s2-1) t2 substr($0,s2+l2)}' file

@Chubler_XL, the last gsub could be replaced with a sub.

@Scrutinizer: Yes, it's about using the right tool for the right job!

1 Like
sed 's/0x00\(..\)\(..\)\(..\)\(..\)\(..\)/\1:\2:\3:\4:\5/' file
1 Like

First one didn't return any output.
elixir_sinari's code & Scrutinizer's code worked very fine. The sed one being small code is nice .

Thanks a lot,

Just realized the sed could be shortened a little:

sed 's/0x00\(..\)\(..\)\(..\)\(..\)/\1:\2:\3:\4:/' file

Scrutinizer,

This works nicely: can you please explain how the regular expression working in this case,

sed 's/0x00\(..\)\(..\)\(..\)\(..\)/\1:\2:\3:\4:/' file

Thanks a lot..

Hi, it will substitute a string that consists of "0x00" followed by eight characters. It will group those 8 characters into 4x2 characters (the last 2 characters of the 0x00 address are not being substituted, but left as-is). In the right hand side of the substitute command, it references these 4 groups and places colons between them and behind the last one ( i.e. in front of the 2 characters of the 0x00 address that were left out of the match )

1 Like

Scrutinizer,
This is a grat explanation of the great code, I am able to understand it, never thought it is such a magic code, to convert that into colon separated,

sed 's/0x00\(..\)\(..\)\(..\)\(..\)/\1:\2:\3:\4:/' file

Thanks for the explanation as well,
Reveri,