row to column and position data in to fixed column width

Dear friends,

Below is my program and current output. I wish to have 3 or 4 column output in order to accomodate in single page. i do have subsequent command to process after user enter the number.

Program

COUNT=1
for MYDIR in `ls /`
do
  VOBS[${COUNT}]=${MYDIR}
  echo "${COUNT}. ${MYDIR}"
  COUNT=expr ${COUNT} + 1
done

Current Output

 
1. BA
2. GTE
3. IBM
4. VB
5. file
6. folder
8. kernal
9. lost+found
10. mnt
11. net
12. opt
13. platform
14. proc
15. profile.new
16. sbin
17. scripts
18. tmp
19. var
20. vol
21. ......etc

Desired output

I tried used other alternatives like nawk etc. But it displays either separated by tab or space. I would like to print fixed intervals irespective of data width.

Please find attached image

You can achieve this Perl using the following code

opendir DIR,"." or die "Can't open dir : $!\n";
while (my $files=readdir(DIR))
{

if ($files=~/^\./) #this is for avoiding the . and .. default files
{
    next;
}
print "$count) $files\t";
if ( $count % 3 == 0 )
{
    print "\n";
}
$count++;
}

The above code will list all the files in the current directory.
It will display 3 files per line.

The sample output I got is as follows

1) xac  2) dir.pl       3) func.sh
4) xad  5) sc.sh        6) xae
7) xab  8) test.sh      9) split.sh
10) xaa

Hi baluchen,
You can achieve this by some small changes in your script .For the formatted output just use the printf .Check the count according to then move the line ( Here I check for the three elements in a line.

COUNT=1
for MYDIR in `ls /`
do
VOBS[${COUNT}]=${MYDIR}
printf "%-25s" "${COUNT}. ${MYDIR}"
let SPLIT=`expr ${COUNT} % 3`
if [ $SPLIT -eq 0 ]
then
        echo
fi
let COUNT=${COUNT}+1
done

Excellent..

Thanks Karthi and Thillai

It worked as i expected.

ls / |awk '{print NR".", $0}'