Weiss Willy
Weiss Willy

Reputation: 109

CSV file manipulation

The below line represent an simple line of a CVS file that I need to manipulate.

....column-n    column-m
---------------------------------
....0x65AFB4C6, 0x27D47C0

From the above I must produce the next output:

0, 101, 175, 180, 198, 0, 39, 212, 124, 0

In other words I must split each hexadecimal value into bits and each bit to be converted to decimal number comma separated like the following:

0x65AFB4C6 becomes
0x  65     AF     B4    C6 which is converted to decimal
0  ,101  , 175  ,180  , 198

I know how to grep the entire column but how to I split every two characters and convert them to decimal? Any idea?

Upvotes: 0

Views: 51

Answers (1)

KamilCuk
KamilCuk

Reputation: 140960

The following script with comments inside:

# pipe our input
cat <<EOF |
....column-n    column-m
---------------------------------
....0x65AFB4C6, 0x27D47C0
EOF
# first extract the numbers from input
# print the numbers on separate newlines appended with two 00
sed -E 's/0x([[:xdigit:]]+)/\n00\1\n/g' |
# filter lines starting with two 00
grep '^00' |
# make each number have a separate line
sed -E 's/../&\n/g;' |
# remove empty lines, there will be some
sed '/^$/d' |
# convert hex lines into decimal lines
xargs -I{} printf "%d\n" "0x{}" |
# print the lines separated by comma
paste -sd,

Produces the following output:

0,101,175,180,198,0,39,212,124,0

Tested at paiza.io

Upvotes: 2

Related Questions