![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Formatting | mirusnet | Shell Programming and Scripting | 3 | 01-06-2008 11:38 PM |
| Formatting using awk | cdunavent | Shell Programming and Scripting | 2 | 03-18-2003 02:09 PM |
| formatting | tamemi | UNIX for Dummies Questions & Answers | 5 | 07-21-2002 10:03 AM |
| formatting | xeron | UNIX for Dummies Questions & Answers | 5 | 03-20-2002 11:33 AM |
| formatting | darryll777 | UNIX for Dummies Questions & Answers | 1 | 03-04-2002 01:15 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
formatting
I have file with different columns
for ex. contents of file "txt" NAME AGE MARKS HARRY 23 89 TOM 12 67 BOB 23 11 and you see its not formatted.Now, I need the file "txt" to be formatted like COLUMN1 COLUMN2 COLUMN3 NAME AGE MARKS with proper spacing between them... how to do this in shell scripting? |
|
||||
|
A more general solution is to use printf Code:
while read name age marks; do printf "%-12s %2i %s\n" "$name" "$age" "$marks" done <txt It's simpler still if you use awk, because it will loop over each line by itself: Code:
awk '{ printf "%-12s %2i %s\n", $1, $2, $3 }' txt
The widths (here, 12, 2, and unbounded) are obviously up to you to set to suitable values. |
|
|||||
|
Hi.
A production-quality perl script for automatically aligning columns can be found at align: text column alignment filter, written by Steve Kinzler. Although the title is text column ..., it right-adjusts if a field is numeric. There are command-line over-ride options for adjustment direction, along with numerous other features. I find it to be a very useful code ... cheers, drl |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|