LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash scripting: column-ize file of varying length strings (https://www.linuxquestions.org/questions/programming-9/bash-scripting-column-ize-file-of-varying-length-strings-351611/)

Quantum0726 08-09-2005 03:39 PM

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!

leonscape 08-09-2005 04:02 PM

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.

Quantum0726 08-12-2005 04:47 PM

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!

eddiebaby1023 08-13-2005 08:19 AM

Use printf with suitably sized minimum, left justified, strings.

leonscape 08-13-2005 06:19 PM

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


All times are GMT -5. The time now is 04:49 PM.