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 12-03-2006, 04:59 PM   #1
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Rep: Reputation: 30
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??

Last edited by ygloo; 12-26-2006 at 06:06 PM.
 
Old 12-03-2006, 05:46 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 03-04-2007, 12:35 PM   #3
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Original Poster
Rep: Reputation: 30
insted of script now i use
mount | column -t
 
  


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
is a printf wrapper possible? Thinking Programming 2 10-21-2005 07:44 AM
Can we specify variable field width in a scanf() format string? skie_knite007 Programming 3 05-13-2005 12:56 PM
More arguments in printf() AMMullan Programming 3 02-23-2004 02:29 PM
Using 'printf' on a 'struct' skywalker27182 Programming 10 12-20-2003 12:06 PM
printf new_user10 Programming 3 09-09-2003 11:12 PM

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

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