0

I am looking for a solution to find search pattern pair.

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)

Above is the input

Desired Output is

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

Which is Type and Retention Level pair. So Under each Type: there is a Retention Level. I need all the pairs in desired output.

Additional Input file where more than 2 pairs of Type and Retention is available:

`  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
    can you crosscheck your desired output, it may be Full Backup;3 weeks,Differential Incremental Backup , 3 weeks; – msp9011 Aug 28 '18 at 7:34
  • Actually I want Type in once cell with ; seperation Full Backup; Differential Incremental Backup then , will provide me next cell which is respective retention i.e 3 weeks; 3 weeks – Sid Aug 28 '18 at 9:22
2

This script:

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

produces

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

This one

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

produces

Full Backup; Full Backup; Differential Incremental Backup, 3 months; 1 month; 3 weeks
  • You hope or you fear? I fear you're right. – Gerard H. Pille Aug 28 '18 at 8:42
  • Thanks @Gerard, However I was looking for Type to be clubbed together with semicolon and then after comma retention respective to type. Actually I want Type in once cell with ; seperation Full Backup; Differential Incremental Backup then , will provide me next cell which is respective retention i.e 3 weeks; 3 weeks – Sid Aug 28 '18 at 9:23
  • I gave it another try. – Gerard H. Pille Aug 28 '18 at 11:12
  • Hi Gerard, Could you please explain the solution. And it gives space at the beginning of the output. – Sid Aug 29 '18 at 12:52
  • Hi Gerard, Please explain the solution, also 1 small issue, it only gives 2 pairs, what if there are 4 to 5 similar pairs it's not showing all Type and retention pairs. – Sid Aug 29 '18 at 13:05
1

Try also

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

EDIT: Or

 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.