2014-04-25 60 views
3

注意我是SED的初学者。Linux上的sed命令只匹配1,在Solaris上找到多个匹配项

我们利用sed的命令,看起来在ClearCase命令的输出,并获得用户的名称,以期的:

<clearcase output> | sed -n "/Development streams:/,// s/[^ ]* *Views: *\([^_ ]*\)_.*/\1/p" 

(实施例clearcase的输出:

Project:    project:[email protected]/xxxx/xxxxx_xxxxxxxx0 
Project's mastership: [email protected]/xxxx/xxxxx_xxxxxxxx0 
Project folder:  folder:[email protected]/xxxx/xxxxx_xxxxxxxx0 (RootFolder/Xxxxxxxx/XX0.0) 
Modifiable components:component:[email protected]/xxxx/xxxxxx_xxxxxxxx0 
         component:[email protected]/xxxx/xxxxxx_xxxxxxxx0 
         component:[email protected]/xxxx/xxxxxx_xxxxxxxx0 
         component:[email protected]/xxxx/xxxxxx_xxxxxxxx0 
         component:[email protected]/xxxx/xxxxxx_xxxxxxxx0 

Integration stream: stream:[email protected]/xxxx/xxxxx_xxxxxxxx0 (FI: nemelis) 
Integration views: olduser_xx0.0_xx0000_int - Properties: dynamic ucmview readwrite nshareable_dos 
         nemelis_xx0.0_xx0000_int - Properties: dynamic ucmview readwrite nshareable_dos 
         otheruser_xx0.0_xx0000_int - Properties: dynamic ucmview readwrite nshareable_dos 

Development streams: stream:[email protected]/xxxx/xxxxx_xxxxxxxx0 [unlocked] - No rebase or delivery is pending. 
         Views:nemelis_xx0.0_xx0000 
         stream:[email protected]/xxxx/xxxxx_xxxxxxxx0 [unlocked] - No rebase or delivery is pending. 
         Views:otheruser_xx0.0_xx0000_streamidentifier 

在Solaris将输出:

nemelis

其他用户

但是在(Redhat-)Linux上只给出了第一个名字。我注意到了Stackoverflow,并发现Sed总是在Posix/Gnu上贪婪,并且应该使用Perl(见Non greedy regex matching in sed?)。因此我试图用Perl解决它,但是比我在最后使用“//”,“|”,丢失运算符之前遇到了一些问题,因此我的帖子在这里)

+0

它不回答你的问题,但你可以给ssed (super-sed)一试。 https://launchpad.net/ssed/ – stuXnet

+0

在范围表达式中,范围的第二部分使用空的正则表达式。在这种情况下,空正则表达式使用最后匹配的正则表达式,即替换命令中的正则表达式。由于替换命令在第一个用户的情况下是正确的,所以之后的范围表达式被切换并且由于其在该范围之外而不被看到剩余的用户。如果范围应该是重复的第一个正则表达式,那么范围的第二部分应该是明确的。一个示例测试是'seq 30 | sed -n'/ 5 /,// {s/6/&X/; p}'' – potong

回答

2

不确定你想要实现什么通过指定地址为//。您可能暗示它应该是文件结尾或空白行。前者为$,后者为/^$/

下可能会为你工作:

sed -n "/Development streams:/,$ s/[^ ]* *Views: *\([^_ ]*\)_.*/\1/p" 

从手册:

$

This address matches the last line of the last file of input, or 
the last line of each file when the `-i' or `-s' options are 
specified. 
+0

这是一个现有的脚本,所以我不知道为什么'你的建议可以在Solaris和Linux上运行。感谢名单 – Nemelis