nullByteMe
nullByteMe

Reputation: 6391

Regex to stop at a specific character

I have output that looks like this:

 16    1VirtualMachine1         [aljkas] some_data.blah
 17       Virtual Machine 1        [jklaj] some_more_data.blah
 23    Virtu al Machin e 1      [adwv] some_more_data.blah


 12     Virtual_Machine one      [awa] some_more_data.blah
 11    VirtualMa chineone       [kladfsa] some_more_data.blah

And I want to get just the content between the spaces and up to [.

So output that would return just:

1VirtualMachine1
Virtual Machine 1
Virtu al Machin e 1
Virtual_Machine one
VirtualMa chineone

My regex looks like this (but is not doing what I think it should):

a_cmd | grep -o -E '[[:space:]]{2,}[a-zA-Z0-9\.,_()]+.+[[:space:]]{2,}'

And this just returns all of the original output. What am I doing wrong?

Upvotes: 1

Views: 112

Answers (3)

Ed Morton
Ed Morton

Reputation: 203229

$ awk 'NF{ gsub(/(^[[:space:]]*[[:digit:]]+[[:space:]]+|[[:space:]]+\[.*)/,""); print}' file
1VirtualMachine1
Virtual Machine 1
Virtu al Machin e 1
Virtual_Machine one
VirtualMa chineone

Upvotes: 1

hek2mgl
hek2mgl

Reputation: 157947

Using sed:

sed 's/[0-9 ]* \(.*\)\[.*/\1/;/^$/d' your.file

Upvotes: 3

Kent
Kent

Reputation: 195039

this grep line would do the job:

grep -oP '\d\s+\K[^[]*'

with your example:

kent$  echo " 16    1VirtualMachine1         [aljkas] some_data.blah
 17    Virtual Machine 1        [jklaj] some_more_data.blah
 23    Virtu al Machin e 1      [adwv] some_more_data.blah


 12    Virtual_Machine one      [awa] some_more_data.blah
 11    VirtualMa chineone       [kladfsa] some_more_data.blah"|grep -oP '\d\s+\K[^[]*'
1VirtualMachine1         
Virtual Machine 1        
Virtu al Machin e 1      
Virtual_Machine one      
VirtualMa chineone 

Upvotes: 2

Related Questions