Help with ksh script to display output with specific contents


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with ksh script to display output with specific contents
# 1  
Old 12-13-2011
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.
Code:
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:
Code:
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


Moderator's Comments:
Mod Comment Please use

code tags!

Last edited by vbe; 12-13-2011 at 05:27 AM..
# 2  
Old 12-13-2011
here is the code you needed :-) :-)

Code:
#!/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

Code:
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

# 3  
Old 12-13-2011
perl code:
  1. #! /usr/bin/perl -w
  2. use strict;
  3. my ($ln, @x, @y);
  4. open I, "< inputfile.txt";
  5. printf ("%20s%10s\n", "Storage Group Name", "ALU Name");
  6. for $ln (<I>) {
  7.     if ($ln =~ /Storage Group Name/) {
  8.         chomp (@x = split /:/, $ln);
  9.         $x&#91;1] =~ s/\s+//g;
  10.     }
  11.     elsif ($ln =~ /^\s+[0-9]\s+[0-9]/) {
  12.         chomp (@y = split /\s+/, $ln);
  13.         printf "%20s%10s\n", $x&#91;1], $y[2];
  14.         $x&#91;1] =~ s/$x[1]//g;
  15.     }
  16. }

Output:
Code:
  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

# 4  
Old 12-13-2011
Based on your input file ..
Code:
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

# 5  
Old 12-13-2011
wow how do you guys learn all these... with strange expressions.. like regular expressions... you guys made it all look simple... :-)
# 6  
Old 12-13-2011
Yet another version...
Code:
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
# 7  
Old 12-13-2011
Thanks all for the quick response. It worked.. thanks a lot!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do we display specific row of an output from bottom given line number?

I pass a number to my script. Passing "1" below. ./getfile.sh 1 echo "User entered: $1" ls -ltr *.conf | sed -n '$p' I wish to use ls -ltr i.e list files in ascending order of time the latest showing at the bottom of the output. Number 1 should get me the last row of ls -ltr output i.e... (9 Replies)
Discussion started by: mohtashims
9 Replies

2. UNIX for Dummies Questions & Answers

Display header in script output

Hi -- Working on my own through the book "Learning the KornShell and came to task 4-1, which there is: a script "highest" and it will sort an "album" file. highest filename The author mentions adding a header line to the scripts output if the user types in the -h option. It says "assume the... (9 Replies)
Discussion started by: Decoy Octopus88
9 Replies

3. Homework & Coursework Questions

Using ls or echo to display a specific output

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: What single command line would you enter to get the following output? 8140 drwxr-xr-x 9 root bin 18 Jan 20... (6 Replies)
Discussion started by: dasboot
6 Replies

4. UNIX and Linux Applications

display which line returns specific output

Hi, I'm trying to figure out a way to find which line in my file.txt with IP addresses: 192.168.0.1 192.178.0.2 etc... returns specific result when I execute command affecting all lines. For example when I run: for line in `cat file.txt`; do snmpget $line done it displays the... (5 Replies)
Discussion started by: svetoslav_sj
5 Replies

5. Shell Programming and Scripting

Grep a pattern given in one file at other file and display its corresponding contents as output.

***************************************** Right now i have this current system. I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which... (7 Replies)
Discussion started by: abinash
7 Replies

6. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

7. Shell Programming and Scripting

ksh help assigning specific values to variable in script

Hi - Help needed. I have an input file that looks something like this, but with a lot more entries: A Customer1 B 4500 C 8000 A Customer2 B 6422 C 8922 I need to be able to print details for each customer on one line per customer. ie. if I could print these to a file and then cat... (3 Replies)
Discussion started by: frustrated1
3 Replies

8. Shell Programming and Scripting

Shell script or command help to extract specific contents from a long list of content

Hi, I got a long list of contents: >sequence_1 ASSSSSSSSSSSDDDDDDDDDDDCCCCCCC ASDSFDFFDFDFFWERERERERFSDFESFSFD >sequence_2 ASDFDFDFFDDFFDFDSFDSFDFSDFSDFDSFASDSADSADASD ASDFFDFDFASFASFASFAFSFFSDASFASFASFAFS >sequence_3 VEDFGSDGSDGSDGSDGSDGSDGSDG dDFSDFSDFSDFSDFSDFSDFSDFSDF... (2 Replies)
Discussion started by: patrick87
2 Replies

9. Shell Programming and Scripting

how to display an browser from ksh script

hy, i have an html file file1.html i want to display a browser i.e html file1 from a ksh script ------------------------------------------------------------------- #!/bin/ksh ----------------------------------------------\\ so can u tell me how to display file1.html as a browser.wat... (2 Replies)
Discussion started by: ali560045
2 Replies

10. Shell Programming and Scripting

HTML display timing problem under ksh script

Using a HTML page , i'm running a Unix ksh script with <a href=..>. The script contains loop like this : for i in do rsh..... done each rsh command is running quite long and then i would display results in HTML format but about 5mn my blank page waiting for result is running in error... (1 Reply)
Discussion started by: Nicol
1 Replies
Login or Register to Ask a Question