1

我的输出如下所示:

 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

我只想获取空格之间的内容,最多[.

所以输出只会返回:

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

我的正则表达式看起来像这样(但没有做我认为应该做的事情):

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

这只是返回所有原始输出。我究竟做错了什么?

4

3 回答 3

3

使用sed

sed 's/[0-9 ]* \(.*\)\[.*/\1/;/^$/d' your.file
于 2013-08-27T13:44:03.430 回答
2

这条 grep 行可以完成这项工作:

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

用你的例子:

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 
于 2013-08-27T13:42:14.287 回答
1
$ awk 'NF{ gsub(/(^[[:space:]]*[[:digit:]]+[[:space:]]+|[[:space:]]+\[.*)/,""); print}' file
1VirtualMachine1
Virtual Machine 1
Virtu al Machin e 1
Virtual_Machine one
VirtualMa chineone
于 2013-08-28T04:08:33.730 回答