如何拉出行仅包含特定字符串

时间:2013-11-22 06:39:10

标签: regex perl bash sed awk

我想要一个命令,只输出以“COLOR =”开头的行,然后只有RED,GREEN,YELLOW,BLUE,MAGENTA,CYAN,WHITE或BLACK。每个颜色用“|”分隔,顺序可以改组,指定的颜色可以重复,并且大小写被忽略)。非常感谢任何帮助!

例如,我有一个包含以下行的文件......

COLOUR=ACK|RED|GREEN|YELLOW|BLUE|MAGENTA|CYAN|WHITE|  
COLOUR=ACK|RED|GREEN|YELLOW|BLUE|MAGENTA|CYAN     |WHITE|    TOMORROW  
COLOUR=RED|GREEN|yellow|BLUE|MAGENTA|CYAN|white      
COLOUR=*BL*ACK|RE D|GREEN|YELLOWabc|  
COLOUR=BLACK|green|YELLOW|RED|WHiTE|BLUE|MAGENTA|CYAN  
COLOUR=BLACK|GR  
COLOUR=BLACK  
COLOUR=BLACK|
COLOUR=BLACK|BLACK|BLACK  
COLOUR=RED|RED|RED|BluE|YELLOW|yellow|CYAN|black  
COLOUR=COLOUR = COLOUR  
COLOUR=RED|HELLO|THANKS

这是我需要的输出:

COLOUR=RED|GREEN|yellow|BLUE|MAGENTA|white  
COLOUR=BLACK|green|YELLOW|RED|WHiTE|BLUE|MAGENTA|CYAN  
COLOUR=BLACK  
COLOUR=BLACK|BLACK|BLACK  
COLOUR=RED|RED|RED|BluE|YELLOW|yellow|CYAN|black  

3 个答案:

答案 0 :(得分:2)

我想你是在COLOUR=之后跟着一个零或多个(RED|GREEN|YELLOW|BLUE|MAGENTA|CYAN|WHITE|BLACK)的序列,接着是一个管道,接着是最后一个颜色,后面跟着一个管道,所有情况 - 不敏感的:

grep -Ei '^COLOUR=((RED|GREEN|YELLOW|BLUE|MAGENTA|CYAN|WHITE|BLACK)\|)*(RED|GREEN|YELLOW|BLUE|MAGENTA|CYAN|WHITE|BLACK)\|?$'

根据您的输入文件,它会生成所需的输出:

COLOUR=RED|GREEN|yellow|BLUE|MAGENTA|CYAN|white
COLOUR=BLACK|green|YELLOW|RED|WHiTE|BLUE|MAGENTA|CYAN
COLOUR=BLACK
COLOUR=BLACK|BLACK|BLACK
COLOUR=RED|RED|RED|BluE|YELLOW|yellow|CYAN|black

答案 1 :(得分:0)

尝试使用以下perl 单行

perl -F'=|\|' -lane 'shift @F eq "COLOUR" and grep {!/RED|GREEN|YELLOW|BLUE|MAGENTA|CYAN|WHITE|BLACK/i} @F or print' file

输出

COLOUR=RED|GREEN|yellow|BLUE|MAGENTA|CYAN|white
COLOUR=BLACK|green|YELLOW|RED|WHiTE|BLUE|MAGENTA|CYAN
COLOUR=BLACK
COLOUR=BLACK|BLACK|BLACK
COLOUR=RED|RED|RED|BluE|YELLOW|yellow|CYAN|black

答案 2 :(得分:0)

$ awk '
    BEGIN{ c="RED|GREEN|YELLOW|BLUE|MAGENTA|CYAN|WHITE|BLACK" }
    toupper($0) ~ "^COLOUR=(" c ")([|](" c "))*$"
' file
COLOUR=RED|GREEN|yellow|BLUE|MAGENTA|CYAN|white
COLOUR=BLACK|green|YELLOW|RED|WHiTE|BLUE|MAGENTA|CYAN
COLOUR=BLACK
COLOUR=BLACK|BLACK|BLACK
COLOUR=RED|RED|RED|BluE|YELLOW|yellow|CYAN|black