Reputation: 8621
Anyone know a standard unix command to format a running SHA1 hex string like this:
344F9DA1EA1859437077CCA38923C67797BDB8F6
into this:
344F9DA1 EA185943 7077CCA3 8923C677 97BDB8F6
Like:
echo "344F9DA1EA1859437077CCA38923C67797BDB8F6" | awk ...
Upvotes: 2
Views: 1745
Reputation: 359845
Using pure Bash (version 3.2 or greater):
hex=344F9DA1EA1859437077CCA38923C67797BD
p='(.{8})'; unset patt; for i in {1..5}; do patt+=$p; done
[[ $hex =~ $patt ]]
string=${BASH_REMATCH[@]:1}
echo "$string" # output: 344F9DA1 EA185943 7077CCA3 8923C677 97BDB8F6
Upvotes: 1
Reputation: 38279
Here's one option with sed
:
echo "344F9DA1EA1859437077CCA38923C67797BDB8F6" | sed -E 's/.{8}/& /g'
(replace any sequence of 8 characters by itself plus one space)
Upvotes: 5
Reputation: 43688
Another one:
echo "344F9DA1EA1859437077CCA38923C67797BDB8F6" | fold -b8 | tr "\n" " "
Upvotes: 2
Reputation: 66059
You can do this in bash without piping at all.
bash$ FOO="344F9DA1EA1859437077CCA38923C67797BDB8F6"
bash$ echo ${FOO:0:8} ${FOO:8:8} ${FOO:16:8} ${FOO:24:8} ${FOO:32:8}
344F9DA1 EA185943 7077CCA3 8923C677 97BDB8F6
Upvotes: 3
Reputation: 7702
To add a space after every eighth character, try:
If the contents are on a single line in a file named FILENAME
:
sed 's/.\{8\}/& /g' FILENAME
Or, if they're split across multiple lines. Again, for a file named FILENAME
:
sed ':a;$!{N;s/\n//;ba;};s/.\{8\}/& /g' FILENAME
To illustrate the difference:
ezra@ubuntu:~$ cat test.file
344F9DA1EA1859437077CCA38923C67797BDB8F6
344F9DA1EA1859437077
ezra@ubuntu:~$ sed ':a;$!{N;s/\n//;ba;};s/.\{8\}/& /g' test.file
344F9DA1 EA185943 7077CCA3 8923C677 97BDB8F6 344F9DA1 EA185943 7077
ezra@ubuntu:~$ sed 's/.\{8\}/& /g' test.file
344F9DA1 EA185943 7077CCA3 8923C677 97BDB8F6
344F9DA1 EA185943 7077
Upvotes: 4
Reputation: 37248
How about
echo "344F9DA1EA1859437077CCA38923C67797BDB8F6" \
| awk '{
printf("%s %s %s %s %s\n",
substr($0,1,8), substr($0,9,8), substr($0,17,8), substr($0,25,8),
substr($0,33,8), substr($0,41,8) )
}
'
I hope this helps.
Upvotes: 1