Let's say I have a file following this pattern
112;167;82
16;104;2
24;163;3
67;195;48
I want each number to be converted in hexadecimal.
70;A7;52
10;68;02
18;A3;03
43;C3;30
printf '%x\n'
does work but only for one instance at once.
CodePudding user response:
while IFS=';' read -a n ; do
printf 'X;' "${n[@]}"
echo
done < file | sed 's/;$//'
read -a
reads each line into an array, settingIFS
sets the separator, i.e. the elements of the array will be the numbers on each line;- The
printf
line prints each element of the array followed by a;
, the trailing semicolon is removed in thesed
command afterwards. echo
adds a newline at the end of each printed array.
CodePudding user response:
bash:
while IFS=';' read -ra line; do
line=$(printf '%x;' "${line[@]}")
echo "${line%;}"
done < file
awk (probably faster for a larger file):
awk '
BEGIN {FS=OFS=";"}
{for (i=1; i<NF; i ) {
$i = sprintf ("%x", $i)
}
print}' my-file
CodePudding user response:
awk -F ';' '{print toupper(sprintf("x;x;x",$1,$2,$3))}' file
Output:
70;A7;52 10;68;02 18;A3;03 43;C3;30
CodePudding user response:
Using sed
$ sed 's/;/ /g;s/.*/printf "x" & /e;s/[a-z0-9]\{2\}/\U&;/2;s/[a-z0-9]\{2\}/\U&;/' input_file
In order for the printf
command to run, ;
needs to be removed after which printf "x"
can be executed.
Output
70;A7;52
10;68;02
18;A3;03
43;C3;30