Splitting records by multiple spaces but not single spaces using awk

Written by James McDonald

October 18, 2011

The default field separator on awk is one or more spaces so for the following vmware command output if you try to awk out a VM name which has spaces you get the first word of the VM name and not the full name. To fix you need to change the field separator in awk to allow for a field separator of 2 or more spaces.

~ # vim-cmd /vmsvc/getallvms
Vmid             Name               
112    Formtrap Windows 2008 x64   [
144    ERP Box                     [
32     EDI Windows 2008            [
80     WinXP Client                [
96     Windows 2008                [

~ # vim-cmd /vmsvc/getallvms  | awk '{ print $2 }'
Name
Formtrap
ERP
EDI
WinXP
Windows

# using the -F option
~ # vim-cmd /vmsvc/getallvms  | awk -F" {2,}" '{ print $2 }'
Name
Formtrap Windows 2008 x64
ERP Box
EDI Windows 2008
WinXP Client
Windows 2008

# or if you like to do it another way
~ # vim-cmd /vmsvc/getallvms  | awk  'BEGIN { FS = " {2,}"} ; { print $2 }'
Name
Formtrap Windows 2008 x64
ERP Box
EDI Windows 2008
WinXP Client
Windows 2008

Reference [1]: http://professionalvmware.com/2009/05/unsupported-console-and-ssh-on-esxi-4/#comment-20943990

1 Comment

  1. Andrew

    It should be noted that you may have to use the –re-interval option for the use of {2,} to work. I’m running Awk 3.1.5 and it doesn’t work without it.

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

You May Also Like…

How to Research a CPU Upgrade

How to Research a CPU Upgrade

Upgrade Time! Doing a lot of VMWare Workstation virtualization to create labs for self-study and training. Finding...