Formatting string output in bash

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    Formatting string output in bash

    Is there a more elegant way to do this:
    Code:
    mktemp -u XXXXXXXXXXXXXXXX | sed 's/\(....\)\(....\)\(....\)\(....\)/\1-\2-\3-\4/g'
    This will output 16 characters with '-' signs between the output like (i.e.): EjSe-Cla4-OaT7-IR26
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    Ok, I did find two alternative to do this:
    Code:
    s=; for (( i=1; i<=4; i++ )) ; do s=$s-`mktemp -u XXXX` ; done; echo ${s:1}
    and
    Code:
    function abc () { mktemp -u XXXX; }; echo `abc`-`abc`-`abc`-`abc`
    But the sed part in my original attempt...
    Can I be written simpler?

    Comment

    • SioSio
      Contributor
      • Dec 2019
      • 272

      #3
      Code:
      mktemp -u XXXXXXXXXXXXXXXX | sed 's/\(....\)/\1-/g'| sed '$s/.$//'
      Use awk. This is not elegant,
      Code:
      mktemp -u XXXXXXXXXXXXXXXX |  awk '{print substr($0,1,4)"-"substr($0,5,4)"-"substr($0,9,4)"-"substr($0,13,4)}'

      Comment

      • Luuk
        Recognized Expert Top Contributor
        • Mar 2012
        • 1043

        #4
        @SioSio: a late reaction....

        and if more than 2 processes are needed, i do like this one more:

        Code:
        uuidgen | base64 | sed 's/\(....\)\(....\)\(....\)\(....\).*/\1-\2-\3-\4/g'

        Comment

        • SioSio
          Contributor
          • Dec 2019
          • 272

          #5
          I recently found this thread and it's late
          Code:
          mktemp -u XXXXXXXXXXXXXXXX | sed 's/\(....\)/\1-/g; $s/.$//'

          Comment

          Working...