The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-05-2008
vijay_0209 vijay_0209 is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 52
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?
  #2 (permalink)  
Old 08-05-2008
danmero danmero is online now Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,441
The fast way will be to replace space by tab's.

Code:
tr ' ' '\t' <  file

  #3 (permalink)  
Old 08-06-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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.
  #4 (permalink)  
Old 08-06-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 717
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:28 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0