0

I am reading file utilization on the server with below command. How can I add the hostname in my output as a first column?

Thanks in advance

df -h | grep % | awk '{OFS="\t";print $6,$5}'

Output:

/apps/inf9b2b  43%
/apps/dbclients        13%
/apps/inf9     77%
1
  • As an aside, on some platforms (and maybe some locale settings on platforms on others) the column names contain a %, so grep % or its awk equivalent won't necessarily serve their presumed purpose of filtering out the header. Perhaps you should filter on line number, not by looking for a % sign? With my answer, that would mean changing /%/ to NR>1 Commented Jun 5, 2018 at 20:01

2 Answers 2

1

This is a simple application of How do I use shell variables in an awk script?

df -h | awk -v hostname="$(hostname)" '/%/ {OFS="\t"; print hostname, $6, $5}'

Note that there's no need for an external grep -- just make your pattern match a condition of the awk statement.

Sign up to request clarification or add additional context in comments.

2 Comments

CNR -- please be certain you copied it exactly as given. See this code running in the virtual machine at ideone.com/hC7m1I -- no syntax error there.
...you'll see that it's including the header line there, but that's because their header uses a % sign; changing it to NR>1 instead of /%/ would avoid the issue.
0

You can do df -h | grep % | awk '{OFS="\t";print "hostname\t" $6,$5}'

1 Comment

hostname /dev/odm 0% <br/> hostname /apps 79% <br/> This it how it looks like now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.