Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Formatting output

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-03-2013
Daniel Gate Daniel Gate is offline
Registered User
 
Join Date: May 2008
Last Activity: 11 February 2015, 8:07 AM EST
Posts: 225
Thanks: 84
Thanked 0 Times in 0 Posts
Formatting output

I have the output like below:


HTML Code:
DEV#:   9  DEVICE NAME: hdisk9  TYPE: 1750500  ALGORITHM:  Load Balance
SERIAL: 68173531021
==========================================================================
Path#      Adapter/Path Name          State     Mode     Select     Errors
    0           fscsi0/path0           OPEN   NORMAL  100920851          0
    1*          fscsi1/path1           OPEN   NORMAL          7          0

DEV#:  10  DEVICE NAME: hdisk10  TYPE: 1750500  ALGORITHM:  Load Balance
SERIAL: 68173531022
===========================================================================
Path#      Adapter/Path Name          State     Mode     Select     Errors
    0           fscsi0/path0           OPEN   NORMAL   75036048          0
    1*          fscsi1/path1           OPEN   NORMAL         63          0

DEV#:  11  DEVICE NAME: hdisk11  TYPE: 1750500  ALGORITHM:  Load Balance
SERIAL: 68173531025
===========================================================================
Path#      Adapter/Path Name          State     Mode     Select     Errors
    0           fscsi0/path0           OPEN   NORMAL  318733022          0
    1*          fscsi1/path1           OPEN   NORMAL          7          0

DEV#:  12  DEVICE NAME: hdisk12  TYPE: 1750500  ALGORITHM:  Load Balance
SERIAL: 68173531026
===========================================================================
Path#      Adapter/Path Name          State     Mode     Select     Errors
    0           fscsi0/path0           OPEN   NORMAL   85608664          0
    1*          fscsi1/path1           OPEN   NORMAL          7          0

DEV#:  13  DEVICE NAME: hdisk13  TYPE: 1750500  ALGORITHM:  Load Balance
SERIAL: 68173531027
===========================================================================
Path#      Adapter/Path Name          State     Mode     Select     Errors
    0           fscsi0/path0           OPEN   NORMAL  202312366          0
    1*          fscsi1/path1           OPEN   NORMAL         21          0

DEV#:  14  DEVICE NAME: hdisk14  TYPE: 1750500  ALGORITHM:  Load Balance
SERIAL: 68173531028
===========================================================================
Path#      Adapter/Path Name          State     Mode     Select     Errors
    0           fscsi0/path0           OPEN   NORMAL  238355812          0
    1*          fscsi1/path1           OPEN   NORMAL          7          0
......
and need to get the formatted output with:

HTML Code:
DEVICE NAME   SERIAL
hdisk9             1021
hdisk10            1022
....
The SERIAL is from the last 4 digit of i.e. 68173531021.

Please advise.
Sponsored Links
    #2  
Old Unix and Linux 02-03-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 27 April 2015, 4:31 PM EDT
Location: Amsterdam
Posts: 10,126
Thanks: 326
Thanked 2,667 Times in 2,385 Posts
Try:


Code:
awk '/DEV/{
  getline p
  split(p,F)
  if(NR==2) print $3 FS $4,F[1]
  print $5, substr(F[2],length(F[2])-3,4) 
}
' OFS='\t' infile

The Following User Says Thank You to Scrutinizer For This Useful Post:
Daniel Gate (02-03-2013)
Sponsored Links
    #3  
Old Unix and Linux 02-03-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 27 April 2015, 3:39 PM EDT
Location: Aachen, Germany
Posts: 5,763
Thanks: 101
Thanked 1,522 Times in 1,431 Posts
If your file structure is always as displayed above, i.e. the serial in in the next line after device name, try
Code:
awk 'BEGIN{print "DEVICE NAME\tSERIAL"} /DEVICE NAME/ {printf "%s\t", $5; getline; print substr($2, length($2)-3)}' file
DEVICE NAME    SERIAL
hdisk9    1021
hdisk10    1022
hdisk11    1025
hdisk12    1026
hdisk13    1027
hdisk14    1028

The Following User Says Thank You to RudiC For This Useful Post:
Daniel Gate (02-03-2013)
    #4  
Old Unix and Linux 02-03-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 27 April 2015, 5:07 PM EDT
Location: Galactic Empire
Posts: 3,402
Thanks: 240
Thanked 1,216 Times in 1,142 Posts
Not much difference from solutions posted above:

Code:
awk -F":" ' BEGIN {
        printf "DEVICE NAME\tSERIAL\n";
} /DEVICE NAME:/ {
        d = $3;
        sub(" ",x,d);
        sub(/[ \t].*/,x,d);
        getline;
        s = substr($2,length($2)-3);
        printf "%s\t\t%s\n", d, s;
} ' filename
DEVICE NAME     SERIAL
hdisk9          1021
hdisk10         1022
hdisk11         1025
hdisk12         1026
hdisk13         1027
hdisk14         1028

The Following User Says Thank You to Yoda For This Useful Post:
Daniel Gate (02-03-2013)
Sponsored Links
    #5  
Old Unix and Linux 02-03-2013
Daniel Gate Daniel Gate is offline
Registered User
 
Join Date: May 2008
Last Activity: 11 February 2015, 8:07 AM EST
Posts: 225
Thanks: 84
Thanked 0 Times in 0 Posts
Thank you! It works perfect!
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Formatting the output arijitsaha Shell Programming and Scripting 2 06-12-2012 07:57 AM
Formatting df output newbie_01 UNIX for Dummies Questions & Answers 4 08-01-2011 05:23 AM
Output formatting . pinga123 Shell Programming and Scripting 5 03-25-2011 08:09 AM
more help with formatting ls output... patrick99e99 Shell Programming and Scripting 1 10-27-2009 01:29 AM
Formatting the output Cameron Shell Programming and Scripting 7 02-15-2002 09:30 AM



All times are GMT -4. The time now is 06:13 PM.