Simpler Navigation coming for Servers and Operating Systems
Coming soon: a much simpler Servers and Operating Systems section of the Community. We will combine many of the older boards, and you won't have to click through so many levels to get at the information you need. If you are looking for an older board and do not find it, check the consolidated boards, as the posts are still there.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Add output of hostname command as the first column with awk

SOLVED
Go to solution
Highlighted
Bijeesh
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
Stephan._1
Trusted Contributor
Solution

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.
kemo
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 }'
Dennis Handly
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
}'
Bijeesh
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.
Bijeesh
Respected Contributor

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

Closing the thread