Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Add output of hostname command as the first column with awk

SOLVED
Go to Solution
Respected Contributor Respected Contributor

Add output of hostname command as the first column with awk

hi,
We need to add output of the hostname command with the following.
df -h | sed -n '/Used/{n;p;}'|awk '{print $4}'

current output is
1.5G

required output is
myhostxyz 1.5G

here myhostxyz is the output of hostname command.
5 REPLIES
Trusted Contributor Trusted Contributor

Re: Add output of hostname command as the first column with awk

> df -h | sed -n '/Used/{n;p;}'|awk '{print $4}'

Hi,
try the following:

df -h | sed -n '/Used/{n;p;}'|awk -v MYHOST=$(hostname) '{print MYHOST " " $4}'

hth
Stephan
Share what you know, learn what you don't.
Trusted Contributor Trusted Contributor

Re: Add output of hostname command as the first column with awk

df -h | sed -n '/Used/{n;p;}'|awk '{ print "'"$HOSTNAME"'", $4 }'
Acclaimed Contributor Acclaimed Contributor

Re: Add output of hostname command as the first column with awk

>... | sed -n '/Used/{n;p;}' | awk '{print $4}'

There is no real need to use sed if you are already using awk.
... | awk -v MYHOST=$(hostname) '
/Used/ {
getline
print MYHOST " " $4
exit
}'
Respected Contributor Respected Contributor

Re: Add output of hostname command as the first column with awk

Hi Team,
All answers gave the exact output which I required.
Thanks for the support.
Highlighted
Respected Contributor Respected Contributor

Re: Add output of hostname command as the first column with awk

Closing the thread