2012-03-05 3 views
0

bashスクリプトを作成する方法を探しています。たとえば、sedまたはawkを使用して、 4. *」。見つかった場合、スクリプトは2つのパターンの間にあるテキストを取得し、テキストをファイルにバックアップし、元の入力ファイルからテキストを削除します。要約すると、私はこの1のように構成されている非常に大規模なMIBファイルの特定の非推奨部分をフィルタリングする方法を探しています:廃止予定のエントリを削除するには、sedまたはawkを使用して大規模な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から廃止されました"。 MIBが検索する範囲の最後の部分として、単語「test」を使用している場合しかし、これが唯一の作品

{ 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 } 

:私は私が近づい以下のawkスクリプトが出ています。次のように実際に探索範囲の終わりは次のとおりです。私がする必要がどのような

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

はラインが「から推奨されていません」を含む直後に行われ、これの最初のインスタンスをスキップして次への検索を続けています発生。以下のように、上記の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の適切な使用は、あなたの問題を解決することができ
+1

これは「私の仕事のための仕事の要件はここにあります」というサイトではありません。 「このコード行は動作していません」レベルでさらに質問を表現してください。 – Bohemian

+1

期待される出力も役立ちます。 – Steve

+0

期待される出力が追加されました。 –

答えて

1

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バー)に変更:

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               */ 
-- /*********************************************************************************/ 


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


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

バックアップ部分はではなく、が含まれています。しかし、それは簡単に追加することができます。スクリプトではテキストを表示しないでください(バックアップとしてanoterファイルに保存する必要がある)が既にマークされているためです。

関連する問題