awkを使用してパターンペアを検索する

awkを使用してパターンペアを検索する

検索パターンのペアを見つけるソリューションを探しています。

awk '/Schedule:/,/Retention Level:/' /tmp/data
  Schedule:              Full
    Type:                Full Backup
    Calendar sched: Enabled
     Included Dates-----------
        Tuesday, Week 1
        Tuesday, Week 2
        Tuesday, Week 3
        Tuesday, Week 4
        Tuesday, Week 5
     Excluded Dates----------
        No specific exclude dates entered
        No exclude days of week entered
    Synthetic:           0
    Checksum Change Detection: 0
    PFI Recovery:        0
    Maximum MPX:         4
    Retention Level:     2 (3 weeks)
  Schedule:              INC
    Type:                Differential Incremental Backup
    Calendar sched: Enabled
     Included Dates-----------
        Sunday, Week 1
        Monday, Week 1
        Wednesday, Week 1
        Thursday, Week 1
        Friday, Week 1
        Saturday, Week 1
        Sunday, Week 2
        Monday, Week 2
        Wednesday, Week 2
        Thursday, Week 2
        Friday, Week 2
        Saturday, Week 2
        Sunday, Week 3
        Monday, Week 3
        Wednesday, Week 3
        Thursday, Week 3
        Friday, Week 3
        Saturday, Week 3
        Sunday, Week 4
        Monday, Week 4
        Wednesday, Week 4
        Thursday, Week 4
        Friday, Week 4
        Saturday, Week 4
        Sunday, Week 5
        Monday, Week 5
        Wednesday, Week 5
        Thursday, Week 5
        Friday, Week 5
        Saturday, Week 5
     Excluded Dates----------
        No specific exclude dates entered
        No exclude days of week entered
    PFI Recovery:        0
    Maximum MPX:         4
    Retention Level:     2 (3 weeks)

上記は入力です。

希望の出力は

Full Backup;Differential Incremental Backup , 3 weeks;3 weeks

これはタイプと保存レベルのペアです。したがって、各タイプの下に保存レベルがあります。必要な出力にすべてのペアが必要です。

2つ以上のタイプと予約のペアを使用できる追加の入力ファイル:

`  Schedule:              Montlhy_Full
    Type:                Full Backup
    Calendar sched: Enabled
     Included Dates-----------
        Wednesday, Week 3
     Excluded Dates----------
        No specific exclude dates entered
        No exclude days of week entered
    Synthetic:           0
    Checksum Change Detection: 0
    PFI Recovery:        0
    Maximum MPX:         4
    Retention Level:     5 (3 months)
  Schedule:              Weekly_Full
    Type:                Full Backup
    Calendar sched: Enabled
     Included Dates-----------
        Wednesday, Week 1
        Wednesday, Week 2
        Wednesday, Week 4
        Wednesday, Week 5
     Excluded Dates----------
        No specific exclude dates entered
        No exclude days of week entered
    Synthetic:           0
    Checksum Change Detection: 0
    PFI Recovery:        0
    Maximum MPX:         4
    Retention Level:     3 (1 month)
  Schedule:              Daily_Inc
    Type:                Differential Incremental Backup
    Calendar sched: Enabled
     Included Dates-----------
        Sunday, Week 1
        Monday, Week 1
        Tuesday, Week 1
        Thursday, Week 1
        Friday, Week 1
        Saturday, Week 1
        Sunday, Week 2
        Monday, Week 2
        Tuesday, Week 2
        Thursday, Week 2
        Friday, Week 2
        Saturday, Week 2
        Sunday, Week 3
        Monday, Week 3
        Tuesday, Week 3
        Thursday, Week 3
        Friday, Week 3
        Saturday, Week 3
        Sunday, Week 4
        Monday, Week 4
        Tuesday, Week 4
        Thursday, Week 4
        Friday, Week 4
        Saturday, Week 4
        Sunday, Week 5
        Monday, Week 5
        Tuesday, Week 5
        Thursday, Week 5
        Friday, Week 5
        Saturday, Week 5
     Excluded Dates----------
        No specific exclude dates entered
        No exclude days of week entered
    PFI Recovery:        0
    Maximum MPX:         4
    Retention Level:     2 (3 weeks)

`

答え1

このスクリプトは次のとおりです。

awk '
  $1 == "Schedule:" { $1 = ""; S = $0 }
  $1 == "Type:" { $1 = ""; T = $0 }
  $1 == "Retention" && $2 == "Level:" {
    sub(/^.*\(/,"")
    sub(/\).*/,"")
    print S ", " T ", " $0
  }' \
  $1

生産する

 Full,  Full Backup, 3 weeks
 INC,  Differential Incremental Backup, 3 weeks

これ

awk '
  BEGIN { SEP = "" }
  # if line starts with 'Type:', remove that label,
  # add separator and remainder of line to T
  $1 == "Type:" { $1 = ""; T = T SEP $0 }
  # Retention level lines, add only what's between
  # brackets to L, set separator to ";"
  $1 == "Retention" && $2 == "Level:" {
    sub(/^.*\(/," ")
    sub(/\).*/,"")
    L = L SEP $0
    if (SEP == "") {
      SEP = ";"
    }
  }
  END {
      sub(/^ */,"",T)
      print T "," L
  }' \
  $1

生産する

Full Backup; Full Backup; Differential Incremental Backup, 3 months; 1 month; 3 weeks

答え2

また試み

awk -F"[:)(]" '/Schedule:/,/Retention Level:/ {if (/Type/) printf "%s, ", $NF; if (/Retention/) print $(NF-1)}' /tmp/data
                Full Backup, 3 weeks
                Differential Incremental Backup, 3 weeks

編集する:または

 awk -F"[:)(]" '/Schedule:/,/Retention Level:/ {if (/Type/) TYP = TYP ";" $NF; if (/Retention/) RET = RET ";" $(NF-1)} END {gsub (/; */, ";", TYP); print substr (TYP, 2) "," substr (RET, 2)}' /tmp/data
Full Backup;Differential Incremental Backup,3 weeks;3 weeks

関連情報