LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Printf df to reformat into a "table" (https://www.linuxquestions.org/questions/linux-newbie-8/printf-df-to-reformat-into-a-table-4175578184/)

nenokmagic 04-23-2016 06:29 PM

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


allend 04-23-2016 09:27 PM

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.

grail 04-24-2016 01:38 AM

@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 :)

nenokmagic 04-24-2016 12:23 PM

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.

nenokmagic 04-24-2016 02:35 PM

Hah well figured it out! Thanks for the help you guys.

MadeInGermany 04-25-2016 11:01 AM

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.


All times are GMT -5. The time now is 12:17 AM.