2

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 ...

6 Answers 6

5

Here's one option with sed:

echo "344F9DA1EA1859437077CCA38923C67797BDB8F6" | sed -E 's/.{8}/& /g'

(replace any sequence of 8 characters by itself plus one space)

Sign up to request clarification or add additional context in comments.

7 Comments

Nice. But I forgot to mention I'm on OSX, and SED doesn't seem to have a -r flag. Any tips?
You probably don't need the -r, but points for a shorter command to Martin!
Changed it to sed -E 's/[0-9A-F]{8}/& /g' which seems to work on both OSX and my Linux box
@Hugo: Just add lots of backslashes for things that don't work. Also, & is the traditional way to refer to the whole match. Maybe something similar to s/[0-9A-F]\{8\}/& /g ? No OSX here.
-r is non-standard. For portability, you can do: sed -e 's/\(.\{8\}\)/\1 /g'
|
4

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

Comments

3

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

1 Comment

good - and with no trailing space ...also: eval echo \${FOO:{0..4}*8:8}
2

Another one:

echo "344F9DA1EA1859437077CCA38923C67797BDB8F6" | fold -b8 | tr "\n" " "

Comments

1

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.

1 Comment

Don't know about OSX 's awk, but in GNU awk: echo "344F9DA1EA1859437077CCA38923C67797BDB8F6" | awk --posix '{gsub("([0-9A-F]){8}","& ");print}'.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.