LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   printf field width (https://www.linuxquestions.org/questions/programming-9/printf-field-width-507203/)

ygloo 12-03-2006 04:59 PM

printf field width
 
hello, here is a code that formats output of "mount" command:
Code:

#!/bin/bash
# mount table format

case "$#" in
0)      cat /etc/mtab | awk 'BEGIN      { print "filesystem mount              type" }
                                        { printf "%10s %-20s %-12s %s\n", $1, $2, $3, $4 }'
        ;;
*)      mount "$@"
        ;;
esac

fields are with defined width, and when i mount something that is wider ( iso image as loop )
it moves the other fields to the right...
i saw that "df" command prints first field and then prints other fields on the next line and they keep their position...
how to do this with "printf", or in other way??

Tinkster 12-03-2006 05:46 PM

Try this one ...


Code:

#!/bin/bash
# mount table format

case "$#" in
0)      cat /etc/mtab | awk 'BEGIN {
                              print "filesystem mount              type"
                            }
                            {
                              line[NR]=$0;
                              if(length($1)>longest){
                                longest = length($1)
                              }
                            } 
                            END{
                              for(i in line){
                                split(line[i],chunks);
                                j="%-"longest+1"s %-20s %-12s %s\n";
                                printf j, chunks[1], chunks[2], chunks[3], chunks[4]
                              }
                            }'
        ;;
*)      mount "$@"
        ;;
esac



Cheers,
Tink

ygloo 03-04-2007 12:35 PM

insted of script now i use
mount | column -t


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