My original text is
11 2 CDTZ - b00264ab
36 37 CDTB - c2330001
I want output text to appear as
11 2 CDTZ - b0:02:64:ab
36 37 CDTB - c2:33:00:01
I have to add a colon after every 2 characters for the string that follows hyphen.
Is there any common text processing command to which I can instruct that
In general, I have to process text before and after a pattern (hyphen here) differently.
My efforts so far:
sed and awk.sed inside an awk then I would set - (hyphen) as field separator in awk and do
sed 's/../&:/g;s/:$//' on $2, then print the whole line using print $0.
ACCEPTED]
Try this,
awk -v OFS='\t' '{gsub(/../,"&:",$NF); sub(/:$/,"",$NF);}1' file
11 2 CDTZ - b0:02:64:ab
36 37 CDTB - c2:33:00:01
gsub(/../,"&:",$NF) will add trailing : to each couple or charecters.sub(/:$/,"",$NF) will remove the unwanted : at end of the last field which was added by previous gsub awk command. Just add >newfile at the end. - Kusalananda
{ print }, which is the same as { print $0 }. Siva could just as well had used sub(...); print at the end of that { ... } block. - Kusalananda
Assuming that the hexadecimal number at the end is always eight characters:
$ sed 's/\(..\)\(..\)\(..\)\(..\)$/\1:\2:\3:\4/' file
11 2 CDTZ - b0:02:64:ab
36 37 CDTB - c2:33:00:01
: between every second characters in the last column. - Kusalananda