Formatting output in columns | 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 in columns

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-23-2009
mohsin.quazi mohsin.quazi is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 8 January 2010, 9:52 AM EST
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Formatting output in columns

I have a file which contains data in below format:



Code:
nbkv28s MgmtReporting -> TradingDesk 1
nbkv28s RMBS -> Credits 178
nbkv28s RMBS -> PassThrough 96
nbkv28s RMBS -> Prepayment 111
nbkv28s RMBS -> RMBSHome 370
nbkv28s RMBS -> TradingStrategy 98
nbkvnze RMBS -> RMBSHome 85
nbkvugn GSF -> ABS 165


I want to fromat this data in to seperate columns as shown below, please help:




Code:
 
nbkv28s     MgmtReporting -> TradingDesk              1
nbkv28s     RMBS -> Credits                                 178
nbkv28s     RMBS -> PassThrough                          96
nbkv28s     RMBS -> Prepayment                           111
nbkv28s     RMBS -> RMBSHome                            370
nbkv28s     RMBS -> TradingStrategy                     98
nbkvnze     RMBS -> RMBSHome                            85
nbkvugn     GSF -> ABS                                      165

I am unable to show the last column properly, it should be well aligned. Even here i cannot show
Sponsored Links
    #2  
Old 08-23-2009
Smiling Dragon's Avatar
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
 
Join Date: Nov 2007
Last Activity: 27 October 2014, 10:04 PM EDT
Location: New Zealand
Posts: 1,044
Thanks: 21
Thanked 26 Times in 25 Posts

Code:
#!/usr/bin/perl -w
while (<STDIN>) {
        if (/^([^\s]*)\s+([^\s]+ -> .*) (\d+)$/) { $first=$1;$second=$2;$third=$3;write }
}

format STDOUT =
@<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @##########
$first,$second,$third
.

More info on format
You can tweak the @<<<< bits to get things laid out as you want them.
Sponsored Links
    #3  
Old 08-23-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 31 August 2014, 7:32 PM EDT
Location: Toronto, Canada
Posts: 2,877
Thanks: 0
Thanked 110 Times in 102 Posts

Does this do what you want? (You may have to adjust the spacing.)


Code:
awk '{ x = $2 " " $3 " " $4; printf "%-10s %-30s %4d\n", $1, x, $5}'

    #4  
Old 08-24-2009
mohsin.quazi mohsin.quazi is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 8 January 2010, 9:52 AM EST
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks a lot !!

I have become fan of awk now..
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 a line of data into columns nistleloy UNIX for Dummies Questions & Answers 3 05-12-2009 05:52 PM
formatting the output ragavhere Shell Programming and Scripting 1 08-11-2008 11:03 PM
Formatting info into columns straight_edge Shell Programming and Scripting 8 02-05-2007 11:51 AM
Formatting output illur81 Shell Programming and Scripting 3 10-13-2005 10:24 AM
Text formatting to 132 columns jmossman UNIX for Dummies Questions & Answers 16 05-06-2002 09:34 PM



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