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: 19 December 2014, 2:13 PM EST
Posts: 224
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 02-03-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 21 December 2014, 4:02 PM EST
Location: Amsterdam
Posts: 9,695
Thanks: 300
Thanked 2,491 Times in 2,230 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: 21 December 2014, 2:24 PM EST
Location: Aachen, Germany
Posts: 4,889
Thanks: 80
Thanked 1,225 Times in 1,151 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: 20 December 2014, 7:00 PM EST
Location: Galactic Empire
Posts: 3,395
Thanks: 240
Thanked 1,211 Times in 1,137 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: 19 December 2014, 2:13 PM EST
Posts: 224
Thanks: 84
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 08:57 AM
Formatting df output newbie_01 UNIX for Dummies Questions & Answers 4 08-01-2011 06:23 AM
Output formatting . pinga123 Shell Programming and Scripting 5 03-25-2011 09:09 AM
more help with formatting ls output... patrick99e99 Shell Programming and Scripting 1 10-27-2009 02:29 AM
Formatting the output Cameron Shell Programming and Scripting 7 02-15-2002 10:30 AM



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