Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    awk programming with printf variable sizes


    Looking to print out 'df -hP' into a readable output.
    I'm getting the 'MAXIMUM' length of the Filesystem field, with a script.
    for elength in `df -hP | sed "s/Mounted on/Mounted_on/" | egrep -v "iso|tmpfs" | awk '{print length($1)}'` ; do
    if (( $elength > $MAX )) ; then

    But I want to do:
    df -hP | awk '{printf "%${MAX}s\t%s\t%s\t%s\t%s\t%s\n", $1, $2, $3, $4, $5, $6}'

    However I get the error:
    awk: (FILENAME=- FNR=1) fatal: arg count with `$' must be > 0

    I very quick Google search didn't give me the results I was looking for. Thought someone here may have done this before OR has a better way of displaying the df information in a human readable format, that is not all 'squished' up, such as
    Filesystem Size Used Avail Use% Mounted_on
    /dev/mapper/vg00-lv_root 2.0G 649M 1.3G 35% /
    /dev/mapper/vg00-lv_tmp 4.9G 139M 4.5G 3% /tmp
    /dev/mapper/vg00-lv_usr 3.0G 2.1G 741M 74% /usr
    /dev/mapper/vg00-lv_opt 2.0G 78M 1.8G 5% /opt
    /dev/mapper/vg00-lv_mrepo 67G 47G 17G 75% /mrepo
    /dev/mapper/vg00-lv_var 9.7G 791M 8.5G 9% /var
    /dev/mapper/vg00-lv_home 4.9G 139M 4.5G 3% /home
    /dev/sda1 190M 27M 154M 15% /boot

    Of course, spacing in these forums doesn't match the spacing I pasted.

    Thank you in advance.

  2. #2
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris

    I use perl script align to format data in columns automatically. In most cases it does the right thing by default. However, one can choose the left-right-center-numeric alignment manually if desired, as in:
    #!/usr/bin/env bash
    # @(#) s1	Demonstrate custom automatic alignment.
    # See:
    # Utility functions: print-as-echo, print-line-with-visual-space, debug.
    # export PATH="/usr/local/bin:/usr/bin:/bin"
    pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
    db() { : ; }
    C=$HOME/bin/context && [ -f $C ] && $C df version
    pl " Results:"
    df -hP |
    sed "s/Mounted on/Mounted_on/" |
    align -alrrrrl
    exit 0
    % ./s1
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
    bash GNU bash 3.2.39
    df (GNU coreutils) 6.10
    version (local) 1.66
    Filesystem Size Used Avail Use% Mounted_on
    /dev/md4    53G  25G   26G  49% /
    tmpfs      1.5G    0  1.5G   0% /lib/init/rw
    udev        10M 812K  9.3M   8% /dev
    tmpfs      1.5G    0  1.5G   0% /dev/shm
    /dev/md0   942M  46M  849M   6% /boot
    /dev/md1    19G 1.3G   17G   8% /home
    /dev/md2   7.6M 1.1M  6.1M  15% /small
    The gutter is 1 by default, but may be changed as desired.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Linux User
    Join Date
    Jun 2012
    SF Bay area
    I think this does what you intended, just change the constant in the "-v MAX=12" flag to whatever you want the maximum field width to be.

    df -hP | awk -v MAX=12 '{ fmt = "%" MAX "s\t%s\t%s\t%s\t%s\t%s\n"; printf fmt, substr( $1, 1, MAX), $2, $3, $4, $5, $6; }'
    You need to specify the field with in the "%s" format to make short ones long enough and the substr() call to truncate the long ones.
    Last edited by cnamejj; 09-14-2012 at 08:16 AM. Reason: I broke the [code] tags again by cut-n-pasting one character to the left, ugh!

  4. $spacer_open

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts