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%
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
JonS
  • 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` – Charles Duffy Jun 05 '18 at 20:01

2 Answers2

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.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • CNR -- please be certain you copied it *exactly* as given. See this code running in the virtual machine at https://ideone.com/hC7m1I -- no syntax error there. – Charles Duffy Jun 05 '18 at 20:29
  • ...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. – Charles Duffy Jun 05 '18 at 20:32
0

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

spectrum
  • 379
  • 4
  • 11