2013-10-09 31 views
0

我想从文件中选择一条代码,例如:猛砸选择的代码的片从文件

server foo4 {  
listen { 
    ipaddr = 127.0.0.1 
    port = 2000 
    type = auth 
    } 
authorize { 
    update control { 
      Cleartext-Password := "bob" 
      } 
    pap 
    } 
authenticate { 
    pap 
    } 
} 

从文件,其中上面的代码并不需要所有部件,但它必须始终在以下形式:

server foo4 { } 

有没有办法使用这种或其他方式的正则表达式?

+0

您是否需要提取文本或只是检测它? –

回答

1

一个的hackish确定这是说的方式:

[[ $(tr -cd '{' < input | wc -c) == $(tr -cd '}' < input | wc -c) ]] && echo "Seems ok" || echo "Not ok" 

请注意,这只是确定的{}输入文件的数量是一样的。它不会用于像这样的输入:

server foo4 {  
listen { 
authorize { 
    } 
    } 
authenticate { 
    pap 
    } 
} 
+0

command grep“[[$(tr -cd'{' Adam

+0

@Adam将命令行中的'input'替换为您的文件名。 – devnull

+0

@Adam为什么你要插入'grep'? – devnull