LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-02-2004, 04:07 AM   #1
WinterSt
LQ Newbie
 
Registered: Aug 2004
Posts: 6

Rep: Reputation: 0
Another easy Bourne Shell Scripting question


It is very simple this time (but not simple enough for me). I have file with data entries seperated by ",".
The files looks like this..
Code:
Anderson,Sarah,02-95437869 
Brown,Sarah,02-99893878 
Couch,David,02-95673456 
Jones,Sarah,02-95671660 
Smith,John,02-93272658 
Williams,Nick,02-98781987
I want to display it with each field placed into left justified columns.
Code:
Anderson(white space)Sarah(white space)02-95437869 
Brown(white space''')Sarah(white space)02-99893878 
Couch(white space''')David(white space)02-95673456 
Jones(white space''')Sarah(white space)02-95671660 
Smith(white space''')John(white space')02-93272658 
Williams(white space)Nick(white space')02-98781987

I thought that I would be able to do this by adding multiple \t between each field, but the command I thought would do it, doesn't work.
cat records.txt | tr "[,]" "[\t*2]"

It only replaces the "," with one "\t".


What could I try to do to display this data in columns, or perhaps a way to add multiple tabs?

P.S has to work in bourne shell (#!/bin/sh).

thanksgsg

Last edited by WinterSt; 09-02-2004 at 04:09 AM.
 
Old 09-02-2004, 04:50 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Another easy homework-question? :)
 
Old 09-02-2004, 04:59 AM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
This one works for me:
Code:
#!/bin/sh

cat records.txt | sed 's/,/        /g'
That big space in there is where you shoud press the TAB key however many times you want.
 
Old 09-02-2004, 05:09 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
However, it doesn't aligns well when fields have too distinct width.

Here's a try:
Code:
#!/bin/sh
awk -F, '
function format(s)
{
        padding="";
        l=length(s);
        while(l<22)
        {
                padding=padding " ";
                l++;
        }
        return(s padding);
}
{
        printf("%s %s %s\n",format($1),format($2),$3);
}' $1
 
Old 09-02-2004, 05:17 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
#!/bin/sh

cat records.txt | sed 's/,/ /g'
That's ugly ;) and uses cat in excess ... how about?

Code:
awk -F, '{printf("%-10s%-10s%-10s\n", $1,$2,$3)}' records.txt
And if you make the 10's just one higher than the length
of the longest string in each column you don't have to
worry about potential breaks of the structure ...

Or this very simple approach :)
Code:
column -t -s, records.txt


Cheers,
Tink

Last edited by Tinkster; 09-02-2004 at 05:18 AM.
 
Old 09-02-2004, 12:38 PM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
You win !
 
Old 09-02-2004, 02:47 PM   #7
tonyfreeman
Member
 
Registered: Sep 2003
Location: Fort worth, TX
Distribution: Debian testing 64bit at home, EL5 32/64bit at work.
Posts: 187

Rep: Reputation: 30
Wow ... that column command is bloody brilliant!
 
Old 09-02-2004, 08:41 PM   #8
WinterSt
LQ Newbie
 
Registered: Aug 2004
Posts: 6

Original Poster
Rep: Reputation: 0
Thank you everyone, and especially Tinkster, my bourne shell does not have the column program, but the awk command you listed worked perfectly.

Thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
'sh' shell - Actually calls legacy Bourne shell, or uses system default? Dtsazza Linux - Software 1 10-28-2005 10:20 AM
Shell scripting question. dragin33 Linux - General 2 08-11-2004 06:17 PM
Shell Scripting Question jester_69 Programming 13 11-05-2003 07:55 PM
Bourne shell if statemenet question Mike_the_Man Programming 3 08-11-2003 03:18 PM
Shell Scripting Question chrisk5527 Linux - General 12 07-09-2003 04:36 PM


All times are GMT -5. The time now is 03:16 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration