需要使用sed或awk解析大型MIB文件以删除不推荐使用的条目



我正在寻找一种方法来创建一个bash脚本,可能使用sed或awk来搜索特定的文本字符串,比如"Deprecatedfrom:4.*"。如果找到了,脚本应该接受在两种模式之间找到的文本,将文本备份到一个文件中,然后从原始输入文件中删除文本。总之,我正在寻找一种方法来过滤掉一个非常大的MIB文件中某些不推荐使用的部分,该文件的结构如下:

-- /*********************************************************************************/
-- /* MIB table for foo 'Something that was once very cool                         */
-- /* Valid from: 4.1.01                                                            */
-- /* Valid to: 4.2                                                                 */
-- /* Deprecated from: 4.2                                                          */
-- /*********************************************************************************/
foo bar foo bar foo bar foo bar foo bar foo bar foo bar  
foo bar foo bar foo bar foo bar foo bar foo bar foo bar  
foo bar foo bar foo bar foo bar foo bar foo bar foo bar  
foo bar foo bar foo bar foo bar foo bar foo bar foo bar      
foo bar foo bar foo bar foo bar foo bar foo bar foo bar  
foo bar foo bar foo bar foo bar foo bar foo bar foo bar  
foo bar foo bar foo bar foo bar foo bar foo bar foo bar 
foo bar foo bar foo bar foo bar foo bar foo bar foo bar 
-- /*********************************************************************************/
-- /* MIB table for bar 'Another thing that was once very cool                         */
-- /* Valid from: 4.2.01                                                            */
-- /* Valid to: 4.3                                                                 */
-- /* Deprecated from: 4.3                                                          */
-- /*********************************************************************************/
foo bar foo bar foo bar foo bar foo bar foo bar foo bar  
foo bar foo bar foo bar foo bar foo bar foo bar foo bar  
foo bar foo bar foo bar foo bar foo bar foo bar foo bar  
foo bar foo bar foo bar foo bar foo bar foo bar foo bar      
foo bar foo bar foo bar foo bar foo bar foo bar foo bar  
foo bar foo bar foo bar foo bar foo bar foo bar foo bar  
foo bar foo bar foo bar foo bar foo bar foo bar foo bar 
foo bar foo bar foo bar foo bar foo bar foo bar foo bar 

因此,在这种情况下,让我们说我想去掉包含"从4.2开始弃用"的部分

{ a[i++ % 5 ]=$0} 
/Deprecated from: 4.2/ {print a[(i-5)%5];print a[(i-4)%5];print a[(i-3)%5];print a[(i-2)%5];i=0}
/Deprecated from: 4.2/,/test/ {if($0 !~ /test/) print }

然而,只有当MIB使用单词"test"作为要搜索的范围的最后一部分时,这才有效。事实上,搜索范围的末尾如下:

-- /*********************************************************************************/

我需要做的是跳过它的第一个实例,它紧跟在包含"Deprecated from"的行之后,然后继续搜索下一个实例。

在使用上述MIB示例并删除所有出现的4.2之后,预期输出如下:

-- /*********************************************************************************/
-- /* MIB table for bar 'Another thing that was once very cool                         */
-- /* Valid from: 4.2.01                                                            */
-- /* Valid to: 4.3                                                                 */
-- /* Deprecated from: 4.3                                                          */
-- /*********************************************************************************/
foo bar foo bar foo bar foo bar foo bar foo bar foo bar  
foo bar foo bar foo bar foo bar foo bar foo bar foo bar  
foo bar foo bar foo bar foo bar foo bar foo bar foo bar  
foo bar foo bar foo bar foo bar foo bar foo bar foo bar      
foo bar foo bar foo bar foo bar foo bar foo bar foo bar  
foo bar foo bar foo bar foo bar foo bar foo bar foo bar  
foo bar foo bar foo bar foo bar foo bar foo bar foo bar 
foo bar foo bar foo bar foo bar foo bar foo bar foo bar 

请参阅上面代码的示例:http://ideone.com/bOQuK

我的问题是我需要搜索的关闭模式

-- /*********************************************************************************/

不是:

-- /test/

有什么想法吗?

正确使用带有awk的RS可以解决您的问题:

awk  'BEGIN{RS="-- /[x2a]*/";} 
{ if(NR%2==0)x= ($0!~/Deprecated from: 4.2/)?1:0;  
  if(x)if(NR%2==0)print RT, $0,RT; else print $0}' yourFile

请参阅下面的测试:

我更改了您示例中的内容(foo-bar),以区分文本和哪个块:

kent$  cat big.txt 
-- /*********************************************************************************/
-- /* MIB table for foo 'Something that was once very cool                         */
-- /* Valid from: 4.1.01                                                            */
-- /* Valid to: 4.2                                                                 */
-- /* Deprecated from: 4.2                                                          */
-- /*********************************************************************************/
          ##
          ####
          ##   #
      #   ##     ##
      ###############
      #################
      #   ## 
       #
      ###
      #
      ####             
      ### ##          #
      ###  ##           
      ###    ##       # 
      ###    ####   ###
      ###      #######
         #
-- /*********************************************************************************/
-- /* MIB table for bar 'Another thing that was once very cool                         */
-- /* Valid from: 4.2.01                                                            */
-- /* Valid to: 4.3                                                                 */
-- /* Deprecated from: 4.3                                                          */
-- /*********************************************************************************/
          ##
          ####
          ##   #
      #   ##     ##
      ###############
      #################
      #   ## 
       #
      ###

       #             # 
      #               #
      #                 
      ##       ##     #
       ###  ### #######
        ######    ###

运行awk行:

kent$  awk  'BEGIN{RS="-- /[x2a]*/";} { if(NR%2==0)x= ($0!~/Deprecated from: 4.2/)?1:0;  if(x)if(NR%2==0)print RT, $0,RT; else print $0}' big.txt
-- /*********************************************************************************/ 
-- /* MIB table for bar 'Another thing that was once very cool                         */
-- /* Valid from: 4.2.01                                                            */
-- /* Valid to: 4.3                                                                 */
-- /* Deprecated from: 4.3                                                          */
 -- /*********************************************************************************/

          ##
          ####
          ##   #
      #   ##     ##
      ###############
      #################
      #   ## 
       #
      ###

       #             # 
      #               #
      #                 
      ##       ##     #
       ###  ### #######
        ######    ###

请注意,备份部分不包括在内。然而它很容易被添加。因为在我的脚本中,文本不应该显示(需要保存到另一个文件作为备份)已经标记。

最新更新