Visit Our UNIX and Linux User Community


Formatting df output


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Formatting df output
# 1  
Old 07-22-2011
Formatting df output

Hi all,

Can anyone please suggest how best to handle output from running df where some of the information for a volume/filesystem spread over two lines?

Some of my volume/filesytem are NFS mounted and when I run a df, the information is spread over the two lines instead of the usual norm which is on a single line.

I have a script that parse the df output and because of this irregular output, my script is failing.

Any suggestion on how best to get around this?

In short, I want to be able to "force" the df output to have one line of information only for each filesystem.

BTW, OS is Solaris and df -h is not available.

Thanks in advance.
# 2  
Old 07-22-2011
Please post the exact input (the output from the df command on your system) and an example of the desired output.
# 3  
Old 08-01-2011
Quote:
Originally Posted by radoulov
Please post the exact input (the output from the df command on your system) and an example of the desired output.

Hi Radoulov,

Thanks for your response. Below is the output when running df -k:

Code:
Filesystem            kbytes    used   avail capacity  Mounted on
/dev/md/dsk/d1       8268461 4133009 4052768    51%    /
/proc                      0       0       0     0%    /proc
mnttab                     0       0       0     0%    /etc/mnttab
fd                         0       0       0     0%    /dev/fd
/dev/md/dsk/d3       8268461 5751241 2434536    71%    /var
swap                 46176776      64 46176712     1%    /var/run
dmpfs                46176712       0 46176712     0%    /dev/vx/dmp
dmpfs                46176712       0 46176712     0%    /dev/vx/rdmp
swap                 53453216 7276504 46176712    14%    /tmp
/dev/md/dsk/d4       16526762 8075725 8285770    50%    /opt
/dev/vx/dsk/dg_volarch/SUNWspro
                     6094894 5173296  860650    86%    /opt/SUNWspro.local
/dev/vx/dsk/dg_volarch/volarch
                     83886080 61130760 22577632    74%    /vx_mnt/volarch
aklns001:/vol/vol_admin/public
                     322355200 291922136 30433064    91%    /nas_mnt/public
/dev/odm                   0       0       0     0%    /dev/odm
aklns002:/vol/vol_admin/prodhome
                     33646592 25395656 8250936    76%    /home/users
/vol/dev/dsk/c0t0d0/sol_10_1008_sparc
                     2599020 2599020       0   100%    /cdrom/sol_10_1008_sparc
aklns003-va2:/vol/vol_aftp/aftp/app
                     48179200 39619484 8559716    83%    /nas_mnt/aklns022/app_aftp
/dev/vx/dsk/dg_ico/prod01_db
                     188743680 136754136 51583424    73%    /vx_mnt/db/prod01
/dev/vx/dsk/dg_ico/prod01_app
                     52428800 31237744 21030000    60%    /vx_mnt/app/prod01

Below is the content of the file if I do df -k and re-direct the output, i.e. for example, df -k > /tmp/x. So the contents of /tmp/x is as below:


Code:
Filesystem            kbytes    used   avail capacity  Mounted on
/dev/md/dsk/d1       8268461 4133009 4052768    51%    /
/proc                      0       0       0     0%    /proc
mnttab                     0       0       0     0%    /etc/mnttab
fd                         0       0       0     0%    /dev/fd
/dev/md/dsk/d3       8268461 5750757 2435020    71%    /var
swap                 45929352      64 45929288     1%    /var/run
dmpfs                45929288       0 45929288     0%    /dev/vx/dmp
dmpfs                45929288       0 45929288     0%    /dev/vx/rdmp
swap                 53205808 7276520 45929288    14%    /tmp
/dev/md/dsk/d4       16526762 8079410 8282085    50%    /opt
/dev/vx/dsk/dg_volarch/SUNWspro 6094894 5173296  860650    86%    /opt/SUNWspro.local
/dev/vx/dsk/dg_volarch/volarch 83886080 61184016 22524792    74%    /vx_mnt/volarch
aklns001:/vol/vol_admin/public 322355200 291933108 30422092    91%    /nas_mnt/public
/dev/odm                   0       0       0     0%    /dev/odm
aklns002:/vol/vol_admin/prodhome 33646592 25395684 8250908    76%    /home/users
/vol/dev/dsk/c0t0d0/sol_10_1008_sparc 2599020 2599020       0   100%    /cdrom/sol_10_1008_sparc
aklns003-va2:/vol/vol_aftp/aftp/app 48179200 39619484 8559716    83%    /nas_mnt/aklns022/app_aftp
/dev/vx/dsk/dg_ico/prod01_db 188743680 136754136 51583424    73%    /vx_mnt/db/prod01
/dev/vx/dsk/dg_ico/prod01_app 52428800 31239768 21027992    60%    /vx_mnt/app/prod01

I've attached the desired output. Preferably, would want to be able to show the df output in GB as well. That means I have to do my own conversion since some of the Solaris/*nix that I have does not have the -h option.

At the moment, what am thinking is using the output file for when I re-directed the output of df -k. Then for each column, count which one has the most number of characters and use that as the basis/limiter to print each column either left-justified or right-justified using awk/printf formatting. Not sure if this is the best way to format the output to the one that I want it to.

Hopefully, the df output to file will always be one line for each. Then I will use this script instead of running the df command itself.

Any suggestion will be much appreciated. Thanks in advance.
Formatting df output-formatted-df-outputgif
# 4  
Old 08-01-2011
If you have saved the output of df -k in a file say infile.txt then try
Code:
awk 'NF==1{s=$0;getline x;print s,x;next}1' infile.txt

Or
Code:
df -k | awk 'NF==1{s=$0;getline;$1=$1;print s,$0;next}1'


Last edited by michaelrozar17; 08-01-2011 at 06:41 AM.. Reason: formatted output
# 5  
Old 08-01-2011
Just exploration of the shell. Assuming your output of df in INPUTFILE:
Code:
`awk '{ 
for (i=1; i<=NF; i++)
  if (length($i)>max[i]) max[i]=length($i) }
END { printf "eval sed \"1s/ on//\"  INPUTFILE | xargs printf \"%%-%ds %%%ds %%%ds %%%ds %%%ds %%-%ds\\\\n\"",
max[1], max[2], max[3], max[4], max[5], max[6]}' INPUTFILE`

 

Previous Thread | Next Thread
Test Your Knowledge in Computers #528
Difficulty: Medium
The Extended ASCII character set adds support for 128 additional characters (numbered 128 through 255) by adding 1 more bit (16 total).
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Formatting the Output

Hi, I am trying to use printf command and format certain output in a specific format as under: While the left side (upto |) of the above format is part of a fixed header function, the right side is where i am expecting data to be printed. However, as seen, Row1 value is reflecting on last... (5 Replies)
Discussion started by: EmbedUX
5 Replies

2. Shell Programming and Scripting

Formatting the output

Hi, I have a file which contents entries in this form. Only in /data4/temp abc.000001 Only in /data4/temp abc.000003 Only in /data4/temp abc.000012 Only in /data4/temp abc.000120 Only in /data4/temp abc.000133 Only in /data4/temp abc.001444 i want to read line by line and format... (2 Replies)
Discussion started by: arijitsaha
2 Replies

3. Shell Programming and Scripting

Output Formatting

Hi Guys I need help removing some lines from output i am receiving from a shell script. Here is the output: http://i52.tinypic.com/10z0fut.png I am trying to remove the output that i have circled. . ${EDW}/extracts/bin/extracts_setup2.sh . ${EDW}/extracts/extracts.conf ... (7 Replies)
Discussion started by: mooey1232003
7 Replies

4. Shell Programming and Scripting

Formatting of output

Hi Experts, I have to create a report for certain audit and my output looks as follows I m trying to format my output to look like Any inputs would be highly appreciated Thanks Syed (5 Replies)
Discussion started by: maverick_here
5 Replies

5. Shell Programming and Scripting

formatting output

Sorry for being a n00b, but I'm having a lot more trouble than I should with formatting the output to the program I finally completed. I'm basically looking for the linux equivalent to setw( ) from c++ so that I can print things in columns like this (but without the underlines lol): MISSPELLED: ... (4 Replies)
Discussion started by: aikaterinimak
4 Replies

6. Shell Programming and Scripting

Formatting ls output

I am using find and ls to search for "warez" files on my server. find /home/ -regex ".*\.\(avi\|mp3\|mpeg\|mpg\|iso\)" -print0 | xargs -0 ls -oh This command produces this: -rw-r--r-- 1 1000 3.2M Feb 18 2009 /home/user/public_html/lupus.mp3 I want to only get this 3.2M... (4 Replies)
Discussion started by: bonrad
4 Replies

7. Shell Programming and Scripting

more help with formatting ls output...

Ok, for a fun project, my goal is to replicate the style of "catalog" on an old apple ] *A 002 SOMEAPPLESOFTFILE B 004 SOMEFILE T 006 SOMETEXT I 002 SOMEINTEGERFILE The first character is either " " or "*" depending on if the file is locked or not. Next is the filetype, so in... (1 Reply)
Discussion started by: patrick99e99
1 Replies

8. Shell Programming and Scripting

Formatting Output

Hi I tried running the below awk 'BEGIN { printf ("%s %-51s %s %-7s %s",$var1,$var2,$var3,$var4,$var5)}' from the command prompt and it is not working. Getting the error awk: Field $() is not correct. The source line number is 1. Actually my requirement is to form a string based on... (6 Replies)
Discussion started by: dhanamurthy
6 Replies

9. Shell Programming and Scripting

formatting output

Hi need some advice.. #grep -i hostname test.csv (gives the below output) HOSTNAME,name,host_test,,,,,,,, Now I need to format the above output as below. HOSTNAME: name=host_test Any easy way of doing this using awk or sed or printf? (4 Replies)
Discussion started by: balaji_prk
4 Replies

10. Shell Programming and Scripting

Formatting the output

Hi all, Have the following code(1) producing the results(2 & 3). Would like to know if there is a way to format the two reports created in a similar fashion. IE - The first is formatted nicely as a result of the echo "$xmpbdate $xavgs" >> $xmpbrpt However when I attempt to do the same on... (7 Replies)
Discussion started by: Cameron
7 Replies

Featured Tech Videos