By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,562 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

Formatting string output in bash

Expert 100+
P: 1,038
Is there a more elegant way to do this:
Expand|Select|Wrap|Line Numbers
  1. 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
Aug 19 '17 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 1,038
Ok, I did find two alternative to do this:
Expand|Select|Wrap|Line Numbers
  1. s=; for (( i=1; i<=4; i++ )) ; do s=$s-`mktemp -u XXXX` ; done; echo ${s:1}
  2.  
and
Expand|Select|Wrap|Line Numbers
  1. function abc () { mktemp -u XXXX; }; echo `abc`-`abc`-`abc`-`abc`
  2.  
But the sed part in my original attempt...
Can I be written simpler?
Aug 19 '17 #2

P: 93
Expand|Select|Wrap|Line Numbers
  1. mktemp -u XXXXXXXXXXXXXXXX | sed 's/\(....\)/\1-/g'| sed '$s/.$//'
  2.  
Use awk. This is not elegant,
Expand|Select|Wrap|Line Numbers
  1. mktemp -u XXXXXXXXXXXXXXXX |  awk '{print substr($0,1,4)"-"substr($0,5,4)"-"substr($0,9,4)"-"substr($0,13,4)}'
  2.  
4 Weeks Ago #3

Expert 100+
P: 1,038
@SioSio: a late reaction....

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

Expand|Select|Wrap|Line Numbers
  1. uuidgen | base64 | sed 's/\(....\)\(....\)\(....\)\(....\).*/\1-\2-\3-\4/g'
4 Weeks Ago #4

P: 93
I recently found this thread and it's late
Expand|Select|Wrap|Line Numbers
  1. mktemp -u XXXXXXXXXXXXXXXX | sed 's/\(....\)/\1-/g; $s/.$//'
  2.  
4 Weeks Ago #5

Post your reply

Sign in to post your reply or Sign up for a free account.