我的输出看起来像这样:
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,}'
这只会返回所有原始输出。我做错了什么?
答案 0 :(得分:3)
使用sed
:
sed 's/[0-9 ]* \(.*\)\[.*/\1/;/^$/d' your.file
答案 1 :(得分: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
答案 2 :(得分:1)
$ awk 'NF{ gsub(/(^[[:space:]]*[[:digit:]]+[[:space:]]+|[[:space:]]+\[.*)/,""); print}' file
1VirtualMachine1
Virtual Machine 1
Virtu al Machin e 1
Virtual_Machine one
VirtualMa chineone