Formatting output | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Formatting output

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-03-2013
Daniel Gate Daniel Gate is offline
Registered User
 
Join Date: May 2008
Last Activity: 8 October 2014, 8:14 AM EDT
Posts: 218
Thanks: 78
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 02-03-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 24 October 2014, 12:07 PM EDT
Location: Amsterdam
Posts: 9,546
Thanks: 285
Thanked 2,426 Times in 2,174 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 02-03-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 24 October 2014, 7:30 AM EDT
Location: Aachen, Germany
Posts: 4,424
Thanks: 73
Thanked 1,081 Times in 1,019 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 02-03-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 22 October 2014, 8:12 PM EDT
Location: Galactic Empire
Posts: 3,387
Thanks: 235
Thanked 1,209 Times in 1,135 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 02-03-2013
Daniel Gate Daniel Gate is offline
Registered User
 
Join Date: May 2008
Last Activity: 8 October 2014, 8:14 AM EDT
Posts: 218
Thanks: 78
Thanked 0 Times in 0 Posts
Thank you! It works perfect!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 12:07 PM.