Score:0

Purely parse the column from the bash command output

ng flag

We need to extract the column from the command output. I have tried the methods using awk, and cut commands. Whereas we have the spaces in the 2nd column values due to which delimiter for space or other character does not parse the 2nd column output in a correct way. Do we have another method to get purely the second column of the output as mentioned above?

# cat info.txt
Vmid      Name                                                 File                                 
369    DO-NOT-DELL                          [datastore1] DO-NOT-DELL/DO-NOT-DELL.vmx                                
389    VMware vCenter Server                [datastore1] VMware vCenter Server/VMware vCenter Server.vmx            
390    Auth-vcenter-Don't delete            [datastore1] Auth-vcenter-Don't delete/Auth-vcenter-Don't delete.vmx    
393    VirtualMachine                       [datastore1] VirtualMachine/VirtualMachine.vmx                                            
9      Server 2005 for VM (dev team)        [datastore1] Server 2005 for VM (dev team)      

# cat info.txt | awk '{print $2}'
DO-NOT-DELL
VMware
Auth-vcenter-Don't
VirtualMachine
Server

As in the above output of a 2nd column we don't have the complete value as it in the original file.

Score:4
us flag

It's possible to parse fixed-width columns with GNU awk:

The splitting of an input record into fixed-width fields is specified by assigning a string containing space-separated numbers to the built-in variable FIELDWIDTHS. Each number specifies the width of the field, including columns between fields. If you want to ignore the columns between fields, you can specify the width as a separate field that is subsequently ignored.

% awk -v FIELDWIDTHS="7 37 *" '{print $2}' foo
   Name                              
DO-NOT-DELL                          
VMware vCenter Server                
Auth-vcenter-Don't delete            
VirtualMachine                       
Server 2005 for VM (dev team)        

You can also use a regular expression as the field separator to split the text, so, e.g., using 3 or more spaces as the FS:

% awk -F ' {3,}' '{print $2}' foo
Name
DO-NOT-DELL
VMware vCenter Server
Auth-vcenter-Don't delete
VirtualMachine
Server 2005 for VM (dev team)
cybermizz avatar
ng flag
Thank you so much @muru
bac0n avatar
cn flag
`'NR>1 {print $2}'`, skips the header.
Score:1
cn flag

Convert spaces to tabs with non-default tab stop positions:

unexpand -t 7,44 info.txt | cut -f2
elmclose avatar
cn flag
Excellent solution. I learnt something new : unexpand
Score:1
ng flag

I also found a good approach from one of my friend to get the list of the virtual machines.

# cat info.txt | tr -s " " | cut -d "[" -f1 | cut -d " " -f2- | tail -n +2

                        
DO-NOT-DELL                          
VMware vCenter Server                
Auth-vcenter-Don't delete            
VirtualMachine                       
Server 2005 for VM (dev team)  
Score:0
cn flag

Multiple spaces between 'columns' can be used as awk field separator. This can be done by regex as [ ]{2,}

$ awk -F"[ ]{2,}" '{print $1}'
Vmid
369
389
390
393
9

$ awk -F"[ ]{2,}" '{print $2}'
Name
DO-NOT-DELL
VMware vCenter Server
Auth-vcenter-Don't delete
VirtualMachine
Server 2005 for VM (dev team)

$ awk -F"[ ]{2,}" '{print $3}'
File
[datastore1] DO-NOT-DELL/DO-NOT-DELL.vmx
[datastore1] VMware vCenter Server/VMware vCenter Server.vmx
[datastore1] Auth-vcenter-Don't delete/Auth-vcenter-Don't delete.vmx
[datastore1] VirtualMachine/VirtualMachine.vmx
[datastore1] Server 2005 for VM (dev team)
terdon avatar
cn flag
Your `rev info.txt | awk '{$NF=""; print $0}' | rev | sed 's/\[/SEP\[/g' | awk 'BEGIN{FS="SEP"} {print $1}'` will print `Name File` as the first line. The others seem to be producing something other than what the OP asked for.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.