Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Formatting output in columns

Shell Programming and Scripting


Closed    
 
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 Linux
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: 1 June 2016, 12:57 AM EDT
Location: New Zealand
Posts: 1,060
Thanks: 25
Thanked 29 Times in 28 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: 11 July 2016, 2:55 PM EDT
Location: Toronto, Canada
Posts: 2,898
Thanks: 0
Thanked 134 Times in 118 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.. Linux
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 06:13 PM.