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