LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-23-2016, 07:29 PM   #1
nenokmagic
LQ Newbie
 
Registered: Mar 2016
Posts: 7

Rep: Reputation: Disabled
Printf df to reformat into a "table"


Greetings.

I am currently rather stuck with my objective here, I am attempting to place the information within the df command into a printf command with only a single example from a book that I find rather confusing.

Code:
divider==============================
divider=$divider$divider

header="\n %-10s %8s %10s %11s\n"
t=`\t`

printf "$header" "<Mount Point>" "$t" "<Free Space>" "$t" "<Used Space>" "$t" "<Total Space>"

printf "$divider\n"

echo "`df -h`"
Of course the df -h doesn't work there, but I am simply lost in what I can do. Am I suppose to use a cut command in order to some how puzzle the entire df command into the points or is there an easier way that I am not seeing here?

I attempted to do something here, not sure if this is any help or not.

Code:
IFS=`df -h`
while read Mounted Avail Used Size
do
 printf "%.3s,%-10s,%-20s,%12s\n" $Mounted $Avail $Used $Size
done

Last edited by nenokmagic; 04-23-2016 at 07:41 PM.
 
Old 04-23-2016, 10:27 PM   #2
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,429

Rep: Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348
There is a syntax error in your code, in that you are missing a semicolon in your while;do .. done construction.

One approach is to pipe the output of df -h into the loop
Code:
df -h | while read Mounted Avail Used Size;
do
 printf "%.3s,%-10s,%-20s,%12s\n" $Mounted $Avail $Used $Size
done
Another approach is to use bash redirection.
Code:
while read Mounted Avail Used Size x y;
do
 printf "%.3s,%-10s,%-20s,%12s\n" $Mounted $Avail $Used $Size
done <<<"$(df -h)"
Neither of the above will output as you want, but the difference is instructive.
 
Old 04-24-2016, 02:38 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
@allend - the semi-colon is not required if the 'do' is no the next line

@OP - I would probably use awk as the df data is column oriented, but the while loop option shown by allend should also work just fine. You just need to tweak it to your requirements.

Also, here is another alternative construct:
Code:
while read ...
do
  ...
done< <(df -h)
Please note the space between the two '<' symbols as it is important
 
Old 04-24-2016, 01:23 PM   #4
nenokmagic
LQ Newbie
 
Registered: Mar 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks for the suggestions, they worked excellently.

I decided to go and change it a bit so I got this

Code:
while read File Size Used Avail Use Mounted x y
do
 printf "%.5s\t" $Mounted `\n` $Avail $Used $Size
done <<<"$(df -h)"
Which gives me

Code:
Mount   Avail   Used    Size    ./Cha92.sh: line 5: n: command not found
/dev    1,9G    4,0K    1,9G    ./Cha92.sh: line 5: n: command not found
/run    382M    1,2M    383M    ./Cha92.sh: line 5: n: command not found
/       268G    7,0G    290G    ./Cha92.sh: line 5: n: command not found
/sys/   4,0K    0       4,0K    ./Cha92.sh: line 5: n: command not found
/run/   5,0M    0       5,0M    ./Cha92.sh: line 5: n: command not found
/run/   1,9G    152K    1,9G    ./Cha92.sh: line 5: n: command not found
/run/   100M    56K     100M
What's the problem with the `\n`, i attempted it without \n and it didn't work everything seems as it should be apart from the error getting the way.

Last edited by nenokmagic; 04-24-2016 at 02:34 PM.
 
Old 04-24-2016, 03:35 PM   #5
nenokmagic
LQ Newbie
 
Registered: Mar 2016
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hah well figured it out! Thanks for the help you guys.
 
Old 04-25-2016, 12:01 PM   #6
MadeInGermany
Member
 
Registered: Dec 2011
Posts: 193

Rep: Reputation: 73
Note that an old GNU df needs -P option
Code:
df -hP ...
to ensure that long device names (in column #1) are not printed on an extra line.
 
  


Reply

Tags
bash scripting, printf, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
My find cmd shows a file called "?" how do I reformat it pobrika Linux - General 7 10-08-2015 08:40 AM
[SOLVED] Why doesn't the printf format string "%0x" pad with zeroes on the left? stf92 Programming 2 08-24-2013 06:09 PM
[SOLVED] printf $"Hello $var\n" vs. printf "Hello $var\n" -- not a typo. What is it? GrapefruiTgirl Programming 2 10-21-2010 09:21 AM
"Linked From" table below the "Similar Threads" table? win32sux LQ Suggestions & Feedback 1 06-14-2009 07:34 PM
hard drives come up "no partition table" and "in use by another program" marti972 Linux - Newbie 3 03-17-2009 12:44 AM


All times are GMT -5. The time now is 12:13 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration