df -h command output in HTML format with specific columns
I am trying to write a shell script which sends the output of df -h
in mail with the help of HTML code embedded in the script. The issue is this is printing all six columns. I want only specific sequence (6,2,3,4,5). How do I achieve it? Tried lots of things putting array also ) but still not working.
#! /bin/bash
MailTO=xyz@gmail.com
HOST=`hostname`
Numberofdays=2
ALERT_DIR_COUNT=`df -Ph | grep -v "Use%" | sed 's/%//g' | awk '$5 > 70
{print $1,$2,$3,$4,$5"%",$6;}' | column -t | wc -l`
(
printf "To: xyz@gmail.comn"
printf "Subject:$HOSTn :$ALERT_DIR_COUNTn mounts reached threshold & Logs
Cleared : $Numberofdays daysn"
printf "Content-Type: text/htmln"
printf "<html>n
<body>n"
df -Ph| awk '{ print $6,$2,$3,$4,$5 }''
BEGIN {
print "<table border="8" cellpadding="3" style="border-collapse:
collapse">"
printf "<tr>"
printf "<th bgcolor=turquoise colspan="6">BEFORE_USAGE</th>"
printf "</tr>"
printf "<tr>"
printf "<th bgcolor=gray>MOUNT</th>"
printf "<th bgcolor=gray>SIZE</th>"
printf "<th bgcolor=gray>USED</th>"
printf "<th bgcolor=gray>AVAILABLE</th>"
printf "<th bgcolor=gray>USE%</th>"
printf "</tr>"}
NR>1{
for( i = 1; i <= NF; i++ ) {
printf "%s", "<td bgcolor=azure"
if (i==5&&$i+0>70) printf " bgcolor=azure"
print ">" $i "</td>"
}
print "</tr>"
}
END { print "</table>" }'
text-processing awk html text-formatting
bumped to the homepage by Community♦ 15 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I am trying to write a shell script which sends the output of df -h
in mail with the help of HTML code embedded in the script. The issue is this is printing all six columns. I want only specific sequence (6,2,3,4,5). How do I achieve it? Tried lots of things putting array also ) but still not working.
#! /bin/bash
MailTO=xyz@gmail.com
HOST=`hostname`
Numberofdays=2
ALERT_DIR_COUNT=`df -Ph | grep -v "Use%" | sed 's/%//g' | awk '$5 > 70
{print $1,$2,$3,$4,$5"%",$6;}' | column -t | wc -l`
(
printf "To: xyz@gmail.comn"
printf "Subject:$HOSTn :$ALERT_DIR_COUNTn mounts reached threshold & Logs
Cleared : $Numberofdays daysn"
printf "Content-Type: text/htmln"
printf "<html>n
<body>n"
df -Ph| awk '{ print $6,$2,$3,$4,$5 }''
BEGIN {
print "<table border="8" cellpadding="3" style="border-collapse:
collapse">"
printf "<tr>"
printf "<th bgcolor=turquoise colspan="6">BEFORE_USAGE</th>"
printf "</tr>"
printf "<tr>"
printf "<th bgcolor=gray>MOUNT</th>"
printf "<th bgcolor=gray>SIZE</th>"
printf "<th bgcolor=gray>USED</th>"
printf "<th bgcolor=gray>AVAILABLE</th>"
printf "<th bgcolor=gray>USE%</th>"
printf "</tr>"}
NR>1{
for( i = 1; i <= NF; i++ ) {
printf "%s", "<td bgcolor=azure"
if (i==5&&$i+0>70) printf " bgcolor=azure"
print ">" $i "</td>"
}
print "</tr>"
}
END { print "</table>" }'
text-processing awk html text-formatting
bumped to the homepage by Community♦ 15 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
1
(1) Please don’t say “not working.” What is happening? (2) IIRC, email doesn’t allow multi-line Subject lines. (And it’s probably a good idea to have a space after the colon (:
).) (3) TheBEGIN
block (if any) must be the first thing in anawk
program. (4) Please don’t say'foo''bar'
when you mean'foobar'
unless you have a good reason for doing so (and I can’t think of any). (5) You have an unmatched(
in your script (beforeprintf "To: xyz@gmail.comn"
). … (Cont’d)
– G-Man
Jun 13 '17 at 22:48
1
(Cont’d) … (6) For every line of input, your script first writes fields 6, 2, 3, 4 and 5 (in that order), unadorned, and then prints all the fields (in 1→NF order), wrapped in HTML tags. That’s probably not what you mean. (7) Your script printsbgcolor=azure
unconditionally (for every cell/value), and then, conditionally, again. (8) Your script is very complicated. Simplify it, get something working, and then build on it.
– G-Man
Jun 13 '17 at 22:48
aks: Welcome to U&L on SE ! Please do acknowledge at least the answer you got. If it effectively answers your query, do check the green mark to the left of it (below the point accumulator) so others may later benefit from it. PS: G-Man's comments are well worth acknowledging as well but you are sole judge here.
– Cbhihe
Jun 14 '17 at 16:44
add a comment |
I am trying to write a shell script which sends the output of df -h
in mail with the help of HTML code embedded in the script. The issue is this is printing all six columns. I want only specific sequence (6,2,3,4,5). How do I achieve it? Tried lots of things putting array also ) but still not working.
#! /bin/bash
MailTO=xyz@gmail.com
HOST=`hostname`
Numberofdays=2
ALERT_DIR_COUNT=`df -Ph | grep -v "Use%" | sed 's/%//g' | awk '$5 > 70
{print $1,$2,$3,$4,$5"%",$6;}' | column -t | wc -l`
(
printf "To: xyz@gmail.comn"
printf "Subject:$HOSTn :$ALERT_DIR_COUNTn mounts reached threshold & Logs
Cleared : $Numberofdays daysn"
printf "Content-Type: text/htmln"
printf "<html>n
<body>n"
df -Ph| awk '{ print $6,$2,$3,$4,$5 }''
BEGIN {
print "<table border="8" cellpadding="3" style="border-collapse:
collapse">"
printf "<tr>"
printf "<th bgcolor=turquoise colspan="6">BEFORE_USAGE</th>"
printf "</tr>"
printf "<tr>"
printf "<th bgcolor=gray>MOUNT</th>"
printf "<th bgcolor=gray>SIZE</th>"
printf "<th bgcolor=gray>USED</th>"
printf "<th bgcolor=gray>AVAILABLE</th>"
printf "<th bgcolor=gray>USE%</th>"
printf "</tr>"}
NR>1{
for( i = 1; i <= NF; i++ ) {
printf "%s", "<td bgcolor=azure"
if (i==5&&$i+0>70) printf " bgcolor=azure"
print ">" $i "</td>"
}
print "</tr>"
}
END { print "</table>" }'
text-processing awk html text-formatting
I am trying to write a shell script which sends the output of df -h
in mail with the help of HTML code embedded in the script. The issue is this is printing all six columns. I want only specific sequence (6,2,3,4,5). How do I achieve it? Tried lots of things putting array also ) but still not working.
#! /bin/bash
MailTO=xyz@gmail.com
HOST=`hostname`
Numberofdays=2
ALERT_DIR_COUNT=`df -Ph | grep -v "Use%" | sed 's/%//g' | awk '$5 > 70
{print $1,$2,$3,$4,$5"%",$6;}' | column -t | wc -l`
(
printf "To: xyz@gmail.comn"
printf "Subject:$HOSTn :$ALERT_DIR_COUNTn mounts reached threshold & Logs
Cleared : $Numberofdays daysn"
printf "Content-Type: text/htmln"
printf "<html>n
<body>n"
df -Ph| awk '{ print $6,$2,$3,$4,$5 }''
BEGIN {
print "<table border="8" cellpadding="3" style="border-collapse:
collapse">"
printf "<tr>"
printf "<th bgcolor=turquoise colspan="6">BEFORE_USAGE</th>"
printf "</tr>"
printf "<tr>"
printf "<th bgcolor=gray>MOUNT</th>"
printf "<th bgcolor=gray>SIZE</th>"
printf "<th bgcolor=gray>USED</th>"
printf "<th bgcolor=gray>AVAILABLE</th>"
printf "<th bgcolor=gray>USE%</th>"
printf "</tr>"}
NR>1{
for( i = 1; i <= NF; i++ ) {
printf "%s", "<td bgcolor=azure"
if (i==5&&$i+0>70) printf " bgcolor=azure"
print ">" $i "</td>"
}
print "</tr>"
}
END { print "</table>" }'
text-processing awk html text-formatting
text-processing awk html text-formatting
edited Jun 14 '17 at 3:13
G-Man
13.1k93465
13.1k93465
asked Jun 13 '17 at 22:20
aksaks
62
62
bumped to the homepage by Community♦ 15 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 15 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
1
(1) Please don’t say “not working.” What is happening? (2) IIRC, email doesn’t allow multi-line Subject lines. (And it’s probably a good idea to have a space after the colon (:
).) (3) TheBEGIN
block (if any) must be the first thing in anawk
program. (4) Please don’t say'foo''bar'
when you mean'foobar'
unless you have a good reason for doing so (and I can’t think of any). (5) You have an unmatched(
in your script (beforeprintf "To: xyz@gmail.comn"
). … (Cont’d)
– G-Man
Jun 13 '17 at 22:48
1
(Cont’d) … (6) For every line of input, your script first writes fields 6, 2, 3, 4 and 5 (in that order), unadorned, and then prints all the fields (in 1→NF order), wrapped in HTML tags. That’s probably not what you mean. (7) Your script printsbgcolor=azure
unconditionally (for every cell/value), and then, conditionally, again. (8) Your script is very complicated. Simplify it, get something working, and then build on it.
– G-Man
Jun 13 '17 at 22:48
aks: Welcome to U&L on SE ! Please do acknowledge at least the answer you got. If it effectively answers your query, do check the green mark to the left of it (below the point accumulator) so others may later benefit from it. PS: G-Man's comments are well worth acknowledging as well but you are sole judge here.
– Cbhihe
Jun 14 '17 at 16:44
add a comment |
1
(1) Please don’t say “not working.” What is happening? (2) IIRC, email doesn’t allow multi-line Subject lines. (And it’s probably a good idea to have a space after the colon (:
).) (3) TheBEGIN
block (if any) must be the first thing in anawk
program. (4) Please don’t say'foo''bar'
when you mean'foobar'
unless you have a good reason for doing so (and I can’t think of any). (5) You have an unmatched(
in your script (beforeprintf "To: xyz@gmail.comn"
). … (Cont’d)
– G-Man
Jun 13 '17 at 22:48
1
(Cont’d) … (6) For every line of input, your script first writes fields 6, 2, 3, 4 and 5 (in that order), unadorned, and then prints all the fields (in 1→NF order), wrapped in HTML tags. That’s probably not what you mean. (7) Your script printsbgcolor=azure
unconditionally (for every cell/value), and then, conditionally, again. (8) Your script is very complicated. Simplify it, get something working, and then build on it.
– G-Man
Jun 13 '17 at 22:48
aks: Welcome to U&L on SE ! Please do acknowledge at least the answer you got. If it effectively answers your query, do check the green mark to the left of it (below the point accumulator) so others may later benefit from it. PS: G-Man's comments are well worth acknowledging as well but you are sole judge here.
– Cbhihe
Jun 14 '17 at 16:44
1
1
(1) Please don’t say “not working.” What is happening? (2) IIRC, email doesn’t allow multi-line Subject lines. (And it’s probably a good idea to have a space after the colon (
:
).) (3) The BEGIN
block (if any) must be the first thing in an awk
program. (4) Please don’t say 'foo''bar'
when you mean 'foobar'
unless you have a good reason for doing so (and I can’t think of any). (5) You have an unmatched (
in your script (before printf "To: xyz@gmail.comn"
). … (Cont’d)– G-Man
Jun 13 '17 at 22:48
(1) Please don’t say “not working.” What is happening? (2) IIRC, email doesn’t allow multi-line Subject lines. (And it’s probably a good idea to have a space after the colon (
:
).) (3) The BEGIN
block (if any) must be the first thing in an awk
program. (4) Please don’t say 'foo''bar'
when you mean 'foobar'
unless you have a good reason for doing so (and I can’t think of any). (5) You have an unmatched (
in your script (before printf "To: xyz@gmail.comn"
). … (Cont’d)– G-Man
Jun 13 '17 at 22:48
1
1
(Cont’d) … (6) For every line of input, your script first writes fields 6, 2, 3, 4 and 5 (in that order), unadorned, and then prints all the fields (in 1→NF order), wrapped in HTML tags. That’s probably not what you mean. (7) Your script prints
bgcolor=azure
unconditionally (for every cell/value), and then, conditionally, again. (8) Your script is very complicated. Simplify it, get something working, and then build on it.– G-Man
Jun 13 '17 at 22:48
(Cont’d) … (6) For every line of input, your script first writes fields 6, 2, 3, 4 and 5 (in that order), unadorned, and then prints all the fields (in 1→NF order), wrapped in HTML tags. That’s probably not what you mean. (7) Your script prints
bgcolor=azure
unconditionally (for every cell/value), and then, conditionally, again. (8) Your script is very complicated. Simplify it, get something working, and then build on it.– G-Man
Jun 13 '17 at 22:48
aks: Welcome to U&L on SE ! Please do acknowledge at least the answer you got. If it effectively answers your query, do check the green mark to the left of it (below the point accumulator) so others may later benefit from it. PS: G-Man's comments are well worth acknowledging as well but you are sole judge here.
– Cbhihe
Jun 14 '17 at 16:44
aks: Welcome to U&L on SE ! Please do acknowledge at least the answer you got. If it effectively answers your query, do check the green mark to the left of it (below the point accumulator) so others may later benefit from it. PS: G-Man's comments are well worth acknowledging as well but you are sole judge here.
– Cbhihe
Jun 14 '17 at 16:44
add a comment |
1 Answer
1
active
oldest
votes
To simplify the body, you could use something like this:
df -Ph | awk -f stat.awk
I've extracted the awk parts into an awk script, but the script could easily be inlined.
Where stat.awk is:
BEGIN {
print "<html><body><table border="8" cellpadding="3" style="border-collapse: collapse">"
print "<tr>"
print "<th bgcolor=turquoise colspan="6">BEFORE_USAGE</th>"
print "</tr>"
print "<tr>"
print "<th bgcolor=gray>MOUNT</th>"
print "<th bgcolor=gray>SIZE</th>"
print "<th bgcolor=gray>USED</th>"
print "<th bgcolor=gray>AVAILABLE</th>"
print "<th bgcolor=gray>USE%</th>"
print "</tr>"
}
NR > 1 {
bgcolor=""
if ($5+0 > 70) {
bgcolor=" bgcolor=azure"
}
print "<tr><td>"$6"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td"bgcolor">"$5"</td></tr>"
}
END {
print "</table></body></html>"
}
For me, this produces:
<html><body><table border="8" cellpadding="3" style="border-collapse: collapse">
<tr>
<th bgcolor=turquoise colspan=6>BEFORE_USAGE</th>
</tr>
<tr>
<th bgcolor=gray>MOUNT</th>
<th bgcolor=gray>SIZE</th>
<th bgcolor=gray>USED</th>
<th bgcolor=gray>AVAILABLE</th>
<th bgcolor=gray>USE%</th>
</tr>
<tr><td>/</td><td>465Gi</td><td>402Gi</td><td>62Gi</td><td bgcolor=azure>87%</td></tr>
<tr><td>/dev</td><td>339Ki</td><td>339Ki</td><td>0Bi</td><td bgcolor=azure>100%</td></tr>
<tr><td>/Volumes/MobileBackups</td><td>465Gi</td><td>465Gi</td><td>0Bi</td><td bgcolor=azure>100%</td></tr>
<tr><td>/Volumes/Transcend</td><td>120Gi</td><td>62Gi</td><td>57Gi</td><td>53%</td></tr>
<tr><td>/Volumes/LaCie</td><td>3.6Ti</td><td>701Gi</td><td>3.0Ti</td><td>19%</td></tr>
</table></body></html>
Please note, that this solution breaks when your volume names contain spaces.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f370972%2fdf-h-command-output-in-html-format-with-specific-columns%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To simplify the body, you could use something like this:
df -Ph | awk -f stat.awk
I've extracted the awk parts into an awk script, but the script could easily be inlined.
Where stat.awk is:
BEGIN {
print "<html><body><table border="8" cellpadding="3" style="border-collapse: collapse">"
print "<tr>"
print "<th bgcolor=turquoise colspan="6">BEFORE_USAGE</th>"
print "</tr>"
print "<tr>"
print "<th bgcolor=gray>MOUNT</th>"
print "<th bgcolor=gray>SIZE</th>"
print "<th bgcolor=gray>USED</th>"
print "<th bgcolor=gray>AVAILABLE</th>"
print "<th bgcolor=gray>USE%</th>"
print "</tr>"
}
NR > 1 {
bgcolor=""
if ($5+0 > 70) {
bgcolor=" bgcolor=azure"
}
print "<tr><td>"$6"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td"bgcolor">"$5"</td></tr>"
}
END {
print "</table></body></html>"
}
For me, this produces:
<html><body><table border="8" cellpadding="3" style="border-collapse: collapse">
<tr>
<th bgcolor=turquoise colspan=6>BEFORE_USAGE</th>
</tr>
<tr>
<th bgcolor=gray>MOUNT</th>
<th bgcolor=gray>SIZE</th>
<th bgcolor=gray>USED</th>
<th bgcolor=gray>AVAILABLE</th>
<th bgcolor=gray>USE%</th>
</tr>
<tr><td>/</td><td>465Gi</td><td>402Gi</td><td>62Gi</td><td bgcolor=azure>87%</td></tr>
<tr><td>/dev</td><td>339Ki</td><td>339Ki</td><td>0Bi</td><td bgcolor=azure>100%</td></tr>
<tr><td>/Volumes/MobileBackups</td><td>465Gi</td><td>465Gi</td><td>0Bi</td><td bgcolor=azure>100%</td></tr>
<tr><td>/Volumes/Transcend</td><td>120Gi</td><td>62Gi</td><td>57Gi</td><td>53%</td></tr>
<tr><td>/Volumes/LaCie</td><td>3.6Ti</td><td>701Gi</td><td>3.0Ti</td><td>19%</td></tr>
</table></body></html>
Please note, that this solution breaks when your volume names contain spaces.
add a comment |
To simplify the body, you could use something like this:
df -Ph | awk -f stat.awk
I've extracted the awk parts into an awk script, but the script could easily be inlined.
Where stat.awk is:
BEGIN {
print "<html><body><table border="8" cellpadding="3" style="border-collapse: collapse">"
print "<tr>"
print "<th bgcolor=turquoise colspan="6">BEFORE_USAGE</th>"
print "</tr>"
print "<tr>"
print "<th bgcolor=gray>MOUNT</th>"
print "<th bgcolor=gray>SIZE</th>"
print "<th bgcolor=gray>USED</th>"
print "<th bgcolor=gray>AVAILABLE</th>"
print "<th bgcolor=gray>USE%</th>"
print "</tr>"
}
NR > 1 {
bgcolor=""
if ($5+0 > 70) {
bgcolor=" bgcolor=azure"
}
print "<tr><td>"$6"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td"bgcolor">"$5"</td></tr>"
}
END {
print "</table></body></html>"
}
For me, this produces:
<html><body><table border="8" cellpadding="3" style="border-collapse: collapse">
<tr>
<th bgcolor=turquoise colspan=6>BEFORE_USAGE</th>
</tr>
<tr>
<th bgcolor=gray>MOUNT</th>
<th bgcolor=gray>SIZE</th>
<th bgcolor=gray>USED</th>
<th bgcolor=gray>AVAILABLE</th>
<th bgcolor=gray>USE%</th>
</tr>
<tr><td>/</td><td>465Gi</td><td>402Gi</td><td>62Gi</td><td bgcolor=azure>87%</td></tr>
<tr><td>/dev</td><td>339Ki</td><td>339Ki</td><td>0Bi</td><td bgcolor=azure>100%</td></tr>
<tr><td>/Volumes/MobileBackups</td><td>465Gi</td><td>465Gi</td><td>0Bi</td><td bgcolor=azure>100%</td></tr>
<tr><td>/Volumes/Transcend</td><td>120Gi</td><td>62Gi</td><td>57Gi</td><td>53%</td></tr>
<tr><td>/Volumes/LaCie</td><td>3.6Ti</td><td>701Gi</td><td>3.0Ti</td><td>19%</td></tr>
</table></body></html>
Please note, that this solution breaks when your volume names contain spaces.
add a comment |
To simplify the body, you could use something like this:
df -Ph | awk -f stat.awk
I've extracted the awk parts into an awk script, but the script could easily be inlined.
Where stat.awk is:
BEGIN {
print "<html><body><table border="8" cellpadding="3" style="border-collapse: collapse">"
print "<tr>"
print "<th bgcolor=turquoise colspan="6">BEFORE_USAGE</th>"
print "</tr>"
print "<tr>"
print "<th bgcolor=gray>MOUNT</th>"
print "<th bgcolor=gray>SIZE</th>"
print "<th bgcolor=gray>USED</th>"
print "<th bgcolor=gray>AVAILABLE</th>"
print "<th bgcolor=gray>USE%</th>"
print "</tr>"
}
NR > 1 {
bgcolor=""
if ($5+0 > 70) {
bgcolor=" bgcolor=azure"
}
print "<tr><td>"$6"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td"bgcolor">"$5"</td></tr>"
}
END {
print "</table></body></html>"
}
For me, this produces:
<html><body><table border="8" cellpadding="3" style="border-collapse: collapse">
<tr>
<th bgcolor=turquoise colspan=6>BEFORE_USAGE</th>
</tr>
<tr>
<th bgcolor=gray>MOUNT</th>
<th bgcolor=gray>SIZE</th>
<th bgcolor=gray>USED</th>
<th bgcolor=gray>AVAILABLE</th>
<th bgcolor=gray>USE%</th>
</tr>
<tr><td>/</td><td>465Gi</td><td>402Gi</td><td>62Gi</td><td bgcolor=azure>87%</td></tr>
<tr><td>/dev</td><td>339Ki</td><td>339Ki</td><td>0Bi</td><td bgcolor=azure>100%</td></tr>
<tr><td>/Volumes/MobileBackups</td><td>465Gi</td><td>465Gi</td><td>0Bi</td><td bgcolor=azure>100%</td></tr>
<tr><td>/Volumes/Transcend</td><td>120Gi</td><td>62Gi</td><td>57Gi</td><td>53%</td></tr>
<tr><td>/Volumes/LaCie</td><td>3.6Ti</td><td>701Gi</td><td>3.0Ti</td><td>19%</td></tr>
</table></body></html>
Please note, that this solution breaks when your volume names contain spaces.
To simplify the body, you could use something like this:
df -Ph | awk -f stat.awk
I've extracted the awk parts into an awk script, but the script could easily be inlined.
Where stat.awk is:
BEGIN {
print "<html><body><table border="8" cellpadding="3" style="border-collapse: collapse">"
print "<tr>"
print "<th bgcolor=turquoise colspan="6">BEFORE_USAGE</th>"
print "</tr>"
print "<tr>"
print "<th bgcolor=gray>MOUNT</th>"
print "<th bgcolor=gray>SIZE</th>"
print "<th bgcolor=gray>USED</th>"
print "<th bgcolor=gray>AVAILABLE</th>"
print "<th bgcolor=gray>USE%</th>"
print "</tr>"
}
NR > 1 {
bgcolor=""
if ($5+0 > 70) {
bgcolor=" bgcolor=azure"
}
print "<tr><td>"$6"</td><td>"$2"</td><td>"$3"</td><td>"$4"</td><td"bgcolor">"$5"</td></tr>"
}
END {
print "</table></body></html>"
}
For me, this produces:
<html><body><table border="8" cellpadding="3" style="border-collapse: collapse">
<tr>
<th bgcolor=turquoise colspan=6>BEFORE_USAGE</th>
</tr>
<tr>
<th bgcolor=gray>MOUNT</th>
<th bgcolor=gray>SIZE</th>
<th bgcolor=gray>USED</th>
<th bgcolor=gray>AVAILABLE</th>
<th bgcolor=gray>USE%</th>
</tr>
<tr><td>/</td><td>465Gi</td><td>402Gi</td><td>62Gi</td><td bgcolor=azure>87%</td></tr>
<tr><td>/dev</td><td>339Ki</td><td>339Ki</td><td>0Bi</td><td bgcolor=azure>100%</td></tr>
<tr><td>/Volumes/MobileBackups</td><td>465Gi</td><td>465Gi</td><td>0Bi</td><td bgcolor=azure>100%</td></tr>
<tr><td>/Volumes/Transcend</td><td>120Gi</td><td>62Gi</td><td>57Gi</td><td>53%</td></tr>
<tr><td>/Volumes/LaCie</td><td>3.6Ti</td><td>701Gi</td><td>3.0Ti</td><td>19%</td></tr>
</table></body></html>
Please note, that this solution breaks when your volume names contain spaces.
edited Jun 14 '17 at 8:45
answered Jun 14 '17 at 8:17
XetiusXetius
1213
1213
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f370972%2fdf-h-command-output-in-html-format-with-specific-columns%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
(1) Please don’t say “not working.” What is happening? (2) IIRC, email doesn’t allow multi-line Subject lines. (And it’s probably a good idea to have a space after the colon (
:
).) (3) TheBEGIN
block (if any) must be the first thing in anawk
program. (4) Please don’t say'foo''bar'
when you mean'foobar'
unless you have a good reason for doing so (and I can’t think of any). (5) You have an unmatched(
in your script (beforeprintf "To: xyz@gmail.comn"
). … (Cont’d)– G-Man
Jun 13 '17 at 22:48
1
(Cont’d) … (6) For every line of input, your script first writes fields 6, 2, 3, 4 and 5 (in that order), unadorned, and then prints all the fields (in 1→NF order), wrapped in HTML tags. That’s probably not what you mean. (7) Your script prints
bgcolor=azure
unconditionally (for every cell/value), and then, conditionally, again. (8) Your script is very complicated. Simplify it, get something working, and then build on it.– G-Man
Jun 13 '17 at 22:48
aks: Welcome to U&L on SE ! Please do acknowledge at least the answer you got. If it effectively answers your query, do check the green mark to the left of it (below the point accumulator) so others may later benefit from it. PS: G-Man's comments are well worth acknowledging as well but you are sole judge here.
– Cbhihe
Jun 14 '17 at 16:44