VMware Cloud Community
dwchan
Enthusiast
Enthusiast

Automate Datastore creation with grep, awk, or sed

I am updating my post kickstart to layout my ESX datastore.  This is for my lab environment where I wipe and drop often. I got all of my drives type and label in a simple array

PHYSICAL_DRIVES="SATA_SSD-SSD_VM Samsung-SSD_VSAN HITACHI-SSD_HDD"

What is the proper syntax or tool (grep, sed, or awk) to read in for example "SATA_SSD-SSD_VM" as $input and break it up into two variable where the market is "-"

So $type = would be "SATA" or all character from the start of $input up to the character "-"

and $label = would be "SSD_VM" or all the character after "-" to the end of the $input string

And feedback would be appreciated.  Thank you in advance

Reply
0 Kudos
4 Replies
daphnissov
Immortal
Immortal

You're really just asking about *NIX tooling at this point. If your var is space delimited, you could just break it up into an array, then use tr to split on the dash. This article may help.

Reply
0 Kudos
dwchan
Enthusiast
Enthusiast

I am actually looking to break the variable within the existing array down further.  I can read them in an array within a for next loop, but want to take that single input and break it down into 2 with each of my loop

Reply
0 Kudos
daphnissov
Immortal
Immortal

Right, so it's a 2-step process: Write a for loop that breaks on the space and puts them into an array. Write another for loop that splits on the dash and sends each part into its own var. Then you just consume those vars like you normally would.

Reply
0 Kudos
dwchan
Enthusiast
Enthusiast

IFS command may work, but is not an option inside busybox

Reply
0 Kudos