473,382 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Formatting string output in bash

1,047 Expert 1GB
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
4 5745
Luuk
1,047 Expert 1GB
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
SioSio
272 256MB
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.  
Jan 21 '20 #3
Luuk
1,047 Expert 1GB
@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'
Jan 21 '20 #4
SioSio
272 256MB
I recently found this thread and it's late
Expand|Select|Wrap|Line Numbers
  1. mktemp -u XXXXXXXXXXXXXXXX | sed 's/\(....\)/\1-/g; $s/.$//'
  2.  
Jan 22 '20 #5

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

Similar topics

2
by: Ken Wilson | last post by:
I am writing and .xml file and am not getting the formatting I would like. The portion of the code that is giving me problems is as follows; XmlTextWriter tw = new XmlTextWriter(filename); ...
2
by: | last post by:
Hi All, I am sending an email containing the string data for eg . string subject ="New client"; from my web service. I want that string data to be appearted as bold in the email. I know this...
6
by: Rafael Olarte | last post by:
The goal of this project is to output the following information as follows: 34.5 38.6 4.1 42.4 3.8 close 46.8 4.4 big change. The values of the first colunm are obtain from a file...
1
by: Wayne Deleersnyder | last post by:
Hi All, I was going to write and ask if someone could help me fix the formatting of my output for hash values, but I believe I got it right now. But, because I couldn't find any website or...
10
by: zgfareed | last post by:
My program converts decimals to binary, hex etc. The output of the binary numbers are to be in the form 0000 0000 for all numbers from 0 to 300. Does anyone have any suggestions as how to get the...
1
by: jasonterando | last post by:
I'm using classes generated from xsd.exe. I am serializing them with XmlTextWriter, and everything works fine. Our customer, though, has a silly requirement that dollar amounts (in the .xsd as...
1
by: pronerd | last post by:
Hi, I was wondering if any one knows of an easy way to add escape characters to an existing string in a BASH script. I have a BASH script that is failing when a string is passed with brackets...
9
by: john coltrane | last post by:
Is there way to create a formatted string in a similar that is similar to sprintf? The same for printing, printf? C,D,E,F,G,N,X for currency, decimal, exponential, fixed, general, numerical,...
5
by: bodycode | last post by:
I am writing a dietary application usng Visual C# 2010. I need to output a decimal value that can be truncated to any number of decimal places I want, without rounding at all, on any level. I want...
0
by: Marek Dohojda | last post by:
I need to split an output from a bash script and populate an array with it. So I can manipulate and print that later. Unfortunately I can not find a way to do that. so what I have thus far is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.