Help with ksh script to display output with specific contents

This is Input - starts with Storage Group Name and ends with Shareable and the loop continues all I need is Storage group name and Alu numbers in the below output format requested.

Storage Group Name:    abcd
Storage Group UID:     00:00:000:00:0:0:0
HBA/SP Pairs:
  HBA UID                                          SP Name     SPPort
  -------                                          -------     ------
 00:00:00:0B:02:0A:01:03:01:00:00:0B:02:0A:01:03   SP B         2
 00:00:00:0B:02:0A:01:03:01:00:00:0B:02:0A:01:03   SP A         3
 00:00:00:0B:02:0A:01:03:01:00:00:0B:02:0A:01:03  SP A         2
 00:00:00:0B:02:0A:01:03:01:00:00:0B:02:0A:01:03   SP A         3
 00:00:00:0B:02:0A:01:03:01:00:00:0B:02:0A:01:03  SP A         2
HLU/ALU Pairs:
  HLU Number     ALU Number
  ----------     ----------
    0               2921
    1               2923
    2               2925
    3               2927
    4               2929
    5               2931
Shareable:             YES
Storage Group Name:    efgh
Storage Group UID:     00:00:000:00:0:0:0
HBA/SP Pairs:
  HBA UID                                          SP Name     SPPort
  -------                                          -------     ------
 00:00:00:0B:02:0A:01:03:01:00:00:0B:02:0A:01:03   SP B         2
 00:00:00:0B:02:0A:01:03:01:00:00:0B:02:0A:01:03   SP A         3
 00:00:00:0B:02:0A:01:03:01:00:00:0B:02:0A:01:03  SP A         2

HLU/ALU Pairs:
  HLU Number     ALU Number
  ----------     ----------
    0               111
    1               112
    2               113
    3              114
    4               115
    5               115
    6               116
Shareable:             YES
Storage Group Name:    ijkl
Storage Group UID:     00:00:000:00:0:0:0
HBA/SP Pairs:
  HBA UID                                          SP Name     SPPort
  -------                                          -------     ------
 00:00:00:0B:02:0A:01:03:01:00:00:0B:02:0A:01:03   SP B         2
 00:00:00:0B:02:0A:01:03:01:00:00:0B:02:0A:01:03   SP A         3

HLU/ALU Pairs:
  HLU Number     ALU Number
  ----------     ----------
    0               222
    1               223
    2               224
    3              225
    4               226
Shareable:             YES

I need OUTPUT FORMAT AS BELOW:

Storage Group Name     Alu Number
abcd   2921
         2923
         2925
         2927
         2929
         2931
   
   
   
efgh   111
         112
         113
         114
         115
         116
   
   
ijkl   222
      223
      224
      225
      226
....so on

Any help is greatly appreciated

here is the code you needed :slight_smile: :slight_smile:

#!/bin/sh
flag=0
while read line
do
 
        echo "$line" | grep "Storage Group Name"

        if echo "$line" | grep -q "Shareable"
        then
                flag=0
        fi
        if [ $flag -eq 1 ]
        then
                echo "$line" | awk -F' ' '{print $2}'
        fi
        if echo "$line" | grep -o "ALU Number"
        then
               flag=1
        fi
 
done < share.txt

---------- Post updated at 04:03 PM ---------- Previous update was at 04:02 PM ----------

its output is as below

Storage Group Name:    abcd
ALU Number
----------
2921
2923
2925
2927
2929
2931
Storage Group Name:    efgh
ALU Number
----------
111
112
113
114
115
115
116
Storage Group Name:    ijkl
ALU Number
----------
222
223
224
225
226

#! /usr/bin/perl -w
use strict;
my ($ln, @x, @y);
open I, "< inputfile.txt";
printf ("%20s%10s\n", "Storage Group Name", "ALU Name");
for $ln (<I>) {
if ($ln =~ /Storage Group Name/) {
chomp (@x = split /:/, $ln);
$x[1] =~ s/\s+//g;
}
elsif ($ln =~ /^\s+[0-9]\s+[0-9]/) {
chomp (@y = split /\s+/, $ln);
printf "%20s%10s\n", $x[1], $y[2];
$x[1] =~ s/$x[1]//g;
}
}
close I;

Output:

  Storage Group Name  ALU Name
                abcd      2921
                          2923
                          2925
                          2927
                          2929
                          2931
                efgh       111
                           112
                           113
                           114
                           115
                           115
                           116
                ijkl       222
                           223
                           224
                           225
                           226

Based on your input file ..

for i in $(nawk '/Storage Group Name: /{print $NF}' infile)
do
        sed -n "/Storage Group Name: $i/,/Shareable/p" infile|nawk '/(Storage Group Name:)/{print $NF}/[0-9] [0-9]/{print $2}'
done

wow how do you guys learn all these... with strange expressions.. like regular expressions... you guys made it all look simple... :slight_smile:

Yet another version...

awk 'BEGIN{print "Storage Group Name \tALU Name"}
/Storage Group Name/{printf $NF}/ALU Number/{f=1;getline;next}
/Shareable/{f=0}f{printf("%25s\n",$NF)}' input_file

--ahamed

Thanks all for the quick response. It worked.. thanks a lot!