Example string
CA DA 00 17 11 38 88 C5 03
Desired output
AB 3C 6C 8F DA 88 24 78 6C
Commands attempted
$ tr -dc 0-9A-F < /dev/urandom filename ## prints too many chars
awk '{gsub(length($1)==2,{printf "%02")}}' filename ## syntax doesn't work, unsure how to add hex
$ sed 's/[a-z0-9]\{2\}//g' filename ## only replaces digits, unsure how to add hex as a replacement
I ended up using vim to do a partial conversion for some level of randomization.
:s/\d\d/AA/g
Can anyone provide a working solution?
It would be nice to see solutions (and explanations) leveraging tr/awk/sed for knowledge sharing purposes.
Thanks.
To replace each field with a random 2-digit hex number with awk is just:
$ awk -v seed="$RANDOM" 'BEGIN{srand(seed)} {for (i=1; i<=NF; i++) $i=sprintf("%02X",rand()*256)} 1' file
C7 A1 02 1A 4A 94 95 A0 1E
$ awk -v seed="$RANDOM" 'BEGIN{srand(seed)} {for (i=1; i<=NF; i++) $i=sprintf("%02X",rand()*256)} 1' file
1C 50 A9 D3 8B B0 24 9C 14
Hopefully it's very obvious what it's doing.
Here is an idea on how to get a random hex (mac address?)
awk -v seed=$RANDOM '
BEGIN{
srand(seed);
split("0 1 2 3 4 5 6 7 8 9 A B C D E F",hex," ");
for (i=1; i<=6; i++)
printf "%s%s ",hex[int(rand()*16)+1],hex[int(rand()*16)+1];
print ""
}'
D8 D9 BA 00 6A C6
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.