LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-28-2012, 03:59 AM   #1
lLinux_Newbiel
LQ Newbie
 
Registered: Jan 2012
Posts: 2

Rep: Reputation: Disabled
Formatting output of a file


Hi all,

i'm currently new to shell script and require some help

i need to format the output of a file in a certain way.Basically to align my output.

Example
info1......info2.......info3........info4
info........info.........info..........info
P.S the . indicates whitespaces

is it possible by sed or awk?
Or there is equivalent of ios_base width c++ that can be used?

Thanks in advance
 
Old 01-28-2012, 04:16 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Welcome to LQ,

awk can do what you want. Have a look at those links:
http://www.gnu.org/software/gawk/man...ontrol-Letters
http://www.gnu.org/software/gawk/man...rmat-Modifiers
 
1 members found this post helpful.
Old 01-28-2012, 04:59 AM   #3
lLinux_Newbiel
LQ Newbie
 
Registered: Jan 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks for the links and the man pages =)
I'll work on it

Marked as solved =)

Last edited by lLinux_Newbiel; 01-28-2012 at 07:49 AM.
 
Old 01-28-2012, 05:09 AM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
If your shell is bash, it has a printf built-in function

Just type:
Code:
help printf
man 1 printf
man 3 printf
in a terminal for help

Last edited by Cedrik; 01-28-2012 at 05:10 AM.
 
1 members found this post helpful.
Old 01-29-2012, 12:56 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
First, do as Cedrik suggests, and read the man pages.
If your input data file is uniformly formatted with 4 fields per line, whitespace delimited:
Code:
#! /bin/bash

while read field1 field2 field3 field4; do
    printf "%-12s%-12s%-12s%-12s\n" $field1 $field2 $field3 $field4
done < yourInputDataFile
--- rod.
 
Old 01-31-2012, 08:12 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.


Anyway, also take a look at the column command.
 
1 members found this post helpful.
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
output formatting aruna t Linux - Newbie 4 06-07-2011 04:30 AM
[SOLVED] facing problem in formatting output file smritisingh03 Programming 2 11-11-2010 04:39 PM
Can't figure out the combine and formatting of output to a file HyperTrey Programming 11 12-12-2008 01:40 PM
Formatting output krock923 Programming 2 06-07-2006 07:26 AM
Formatting output of ls doodar Linux - Newbie 29 07-29-2004 01:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration