Showing results for 
Search instead for 
Did you mean: 

Cisco Community Designated VIP Class of 2020

Turn Comma and Hyphen Separated Vlan List into Expanded Comma Separated List

Cisco Employee

Some devices don’t have an API yet, but do have an ssh command line. So of course you can use expect to automate that device.  Recently while writing PowerShell code to automate Nexus 1000V port-profile provisioning using Cygwin expect and ssh I ran into a problem with the allowed vlan list associated to a trunk.

The list came across as comma separated and range hyphenated. I needed to turn the list into a fully expanded comma separated list.

For example suppose the vlan list was

285, 610-614, 719, 1220-1225

What I needed was

285, 610, 611, 612, 613, 614, 719, 1220, 1221, 1222, 1223, 1224, 1225

How can I change what I received into what I needed?

PowerShell has a Range operator .. that I can use to expand the hyphenated vlans. The range operator along with some string functions will get me what I need.

Range operator .. in action

PS C:\Windows\system32> 1..5






How do I apply the Range operator to the clan list that I was able to get from the Nexus 1000V

I’ll set $vlanList to the vlan list that was retuned by the “show port-profile” command at the Nexus 1000V command line.

PS C:\ >$vlanList = “285, 610-614, 719, 1220-1225”

Then I'll manipulate $vlanList and turn it into what I need

PS C:\ > $vlanList.Split(",")| %{if ($_.indexof("-") -gt 0) {$_.Substring(0,$_.IndexOf("-"))..$_.Substring($_.IndexOf("-")+1)} else {$_.Trim()}} | %{$tmp=""} {if ($tmp.Length -eq 0) {$tmp = $_} else {$tmp=$tmp+", "+$_}} {$tmp}

285, 610, 611, 612, 613, 614, 719, 1220, 1221, 1222, 1223, 1224, 1225

PS C:\Windows\system32>

Let’s break it down by examining the code separating by the pipeline

$vlanList.Split(“,”) turns “285, 610-614, 719, 1220-1225” into an array of string objects that are fed into the next command one string object at a time. Resulting in this list of vlans





Next part of the command

%{if ($_.indexof("-") -gt 0) {$_.Substring(0,$_.IndexOf("-"))..$_.Substring($_.IndexOf("-")+1)} else {$_.Trim()}}

For each string, if there is a hyphen in the string then create a new object that replaces the hyphen with the range operator .. using the Substring and IndexOf methods of the input string.

Go from 610-614 to 610..614, when PowerShell passes this to the next foreach it becomes






Interestingly the string method Replace does not cause PowerShell to expanded 610..614 it simply sees it as another string.  Whereas building the string as shown in the command above causes the desired effect.

If no hyphen is found in the string then just pass it though.

285 -> goes through as 285

Next build the $tmp string which is a comma separated list of the expanded vlans

%{$tmp=""} {if ($tmp.Length -eq 0) {$tmp = $_} else {$tmp=$tmp+","+$_}} {$tmp}

% is an alias for ForEach-Object and ForEach-Object supports a Begin, Process, and End script block.

I can set the $tmp string to “” or nothing in the Begin scriptblock of the ForEach-Object process, then the Process scriptblock builds the $tmp string by either setting $tmp to the first vlan when $tmp length is equal to 0 (zero) or by appending a “,” and then the next vlan when $tmp length is longer than 0. Finally after all the ForEach-Object objects have been processed the End scriptblock prints out $tmp. The result is…

285, 610, 611, 612, 613, 614, 719, 1220, 1221, 1222, 1223, 1224, 1225

Of course these numbers don't have to represent clans they can be any list of comma separated, hyphen separated lists of numbers.

If the list contains no vlans, one vlan, or no hyphenated vlans the command will produce the appropriate output.

Hope this helps in your scripting efforts.

CreatePlease to create content
Content for Community-Ad
FusionCharts will render here