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 *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

Squarespace Image Export

To gain continued access to your Squarespace website images after cancelling your subscription you have several...

MySQL 8.x GRANT ALL STATEMENT

-- CREATE CREATE USER 'tgnrestoreuser'@'localhost' IDENTIFIED BY 'AppleSauceLoveBird2024'; GRANT ALL PRIVILEGES ON...

Exetel Opt-Out of CGNAT

If your port forwards and inbound and/or outbound site-to-site VPN's have failed when switching to Exetel due to their...