使用 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

相关内容