LinuxQuestions.org
Visit Jeremy's Blog.
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 08-09-2005, 03:39 PM   #1
Quantum0726
Member
 
Registered: Dec 2003
Location: Milwaukee, WI
Distribution: Gentoo
Posts: 39

Rep: Reputation: 15
Bash scripting: column-ize file of varying length strings


Hi,
First off, sorry for the bad title, I couldn't think of any better way of explaining the situation. I am writing a script that outputs the basename of a file, a '#', and the full file path of several files (the result of a find command) to a file. I would like to separate the file so that the basenames are in one column and the '#'s are all lined up in another column to the right (example as follows):

file1 # /root/file1
filenamelonger # /root/filenamelonger
f # /root/f

Right now I'm just echo'ing '\t\t' between the basename and the '#', but for longer file names or very short file names the '#'s are not in any particular column.

Any ideas on how I could get these to line up?

Thanks much!
 
Old 08-09-2005, 04:02 PM   #2
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
Find the length of the longest basename (${#string} ), and a number of spaces ( say 4 ).

Then all the others simply take the length of the longest basename + 4 and take away the length of the current basename, and add that number of spaces.

It means iterating through the file twice, but it will give you the column result you want.
 
Old 08-12-2005, 04:47 PM   #3
Quantum0726
Member
 
Registered: Dec 2003
Location: Milwaukee, WI
Distribution: Gentoo
Posts: 39

Original Poster
Rep: Reputation: 15
Works perfectly! Unfortunately that does mean running through the list twice to format it, but it looks much better when it's done, so the extra few milliseconds are worth it!

Thanks!
 
Old 08-13-2005, 08:19 AM   #4
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Use printf with suitably sized minimum, left justified, strings.
 
Old 08-13-2005, 06:19 PM   #5
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
That is another possibility, but he'd still need to know the length of the longest string.
 
  


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
Bash Scripting Empty file john8675309 Linux - Software 2 08-08-2007 04:05 PM
Zero-length bash .history file! revmyo Linux - Security 1 12-03-2005 08:09 PM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
bash scripting read from file cadj Programming 2 02-29-2004 10:42 PM
bash scripting - largest # file extension brian0918 Programming 3 06-16-2003 12:06 PM

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

All times are GMT -5. The time now is 12:31 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
Open Source Consulting | Domain Registration