LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 12-09-2009, 12:32 AM   #1
Scratch59
LQ Newbie
 
Registered: Dec 2009
Posts: 2

Rep: Reputation: 0
Multiple Column output for LS


Hello,

I'm sure this is against the rules in some fashion, and its probably not the best way to start my posting life on this forum, but I am at my wits end here and figured I would give this a shot.

I am trying to write a program that will mirror the standard output of LS on a linux machine. Meaning:

Multiple columns
Column widths are dynamic

Alphabetical order is not important for what I am trying to do

My problem is this:

I have parsed all the names of the members of the directory into an array.

When I created the array, I kept track of the longest record I put into it. Then, I divided the width of the terminal window by the longest record and got the worst case number of columns.

The tricky part is that before I start printing the items to the screen, I have to know how many columns I need. The width of each column is dependent on its longest member. That being the case, I can't figure out a programmatic way to check this status.

I know it sounds like I'm just fishing for answers here, but I have been at this for quite a few days now and I figure at this point that I am just overlooking something. If someone could, in the least, point me in the right direction. Google has yeilded little help and small hints are better than nothing.

Thanks, and sorry in advance for being one of "those guys"
 
Old 12-09-2009, 01:14 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Not sure what you mean about status, but in general, you find the worst case item (which you say you've done), then you assume all entries are that long and print accordingly in fixed-width/padded output.
You don't say which lang you are using and it'd be easier to show us your code, but the cmd in eg bash is http://linux.die.net/man/1/printf, base on the C fn of the same name.
http://www.tldp.org/LDP/abs/html/internal.html#EX47

Last edited by chrism01; 12-09-2009 at 06:24 PM.
 
Old 12-09-2009, 01:42 AM   #3
Scratch59
LQ Newbie
 
Registered: Dec 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Sorry for not stating the language, its C, compiling using the GCC compiler on my machine.

If some of the stuff I am saying doesn't quite come through clearly, I am a little tired so I apologize for that.

Are you saying that I should just make all the columns the width of the worst case?

I am using the printf ("%-*s", max_column_width) statement right now, but don't yet have the information I need to feed to it.

When I run ls on this box, the column widths differ based on the longest item in that column, which is what I was having trouble with. Does ls behave that way on all flavors of linux?

My current idea was going to be to just run this whole thing in a loop. I would start with the worst case number of columns and each time the program realized that it could use more columns for the output, it would re-execute. Not the most efficient, but it would get the job done.

Once the items are all organized nicely in arrays, I can print them to the screen.

EDIT: I would post my code, but there are so many (wrong) things going on in it from my experimentations that it wouldn't be much help.

Last edited by Scratch59; 12-09-2009 at 01:43 AM.
 
Old 12-09-2009, 02:41 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by Scratch59 View Post
My current idea was going to be to just run this whole thing in a loop. I would start with the worst case number of columns and each time the program realized that it could use more columns for the output, it would re-execute. Not the most efficient, but it would get the job done.
I can't think of any other way of doing it to get the ls optimal column widths because how else to know which column any particular file will be listed in after the first line? You could examine the source code for ideas. Here is OpenSolaris ls source code (a mere 3,167 lines!)
 
Old 12-09-2009, 06:28 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Well, my soln is the simple one. If you really wanted to get that complex, you could divide the file list into groups; each group as a separate column and use the longest file in each group as the col length, but it's more complex.
You'd have to ensure all the longest names can fit in one line...
You certainly can get the src code for all Linux cmds, so you could use that...
 
  


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
Loop column output handband2 Programming 8 11-05-2008 04:40 PM
How can we config to display facility/priority column in syslog output (RHELAS 4) LiXin Linux - Server 5 04-30-2008 12:09 PM
Delete Multiple Column Headers rduncan10 Programming 3 08-21-2007 02:24 PM
command to sort by floats in fourth column of standard output jhwilliams Linux - Software 4 06-22-2007 02:27 PM
Sorting of multi-column output rytrom Linux - Newbie 11 09-15-2003 12:31 PM


All times are GMT -5. The time now is 03:25 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration