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

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix or Linux Image
Sponsored Links
    #2  
Old Unix and Linux 08-23-2009
Smiling Dragon's Unix or Linux Image
Smiling Dragon Smiling Dragon is offline Forum Advisor  
Disorganised User
 
Join Date: Nov 2007
Last Activity: 2 February 2015, 2:55 PM EST
Location: New Zealand
Posts: 1,048
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 Unix and Linux 08-23-2009
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 5 April 2015, 4:53 PM EDT
Location: Toronto, Canada
Posts: 2,882
Thanks: 0
Thanked 117 Times in 106 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 Unix and Linux 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.. Unix or Linux Image
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 a line of data into columns nistleloy UNIX for Dummies Questions & Answers 3 05-12-2009 04:52 PM
formatting the output ragavhere Shell Programming and Scripting 1 08-11-2008 10:03 PM
Formatting info into columns straight_edge Shell Programming and Scripting 8 02-05-2007 10:51 AM
Formatting output illur81 Shell Programming and Scripting 3 10-13-2005 09:24 AM
Text formatting to 132 columns jmossman UNIX for Dummies Questions & Answers 16 05-06-2002 08:34 PM



All times are GMT -4. The time now is 05:01 AM.