Re: Add output of hostname command as the first column with awk (3275 Views)
Reply
Respected Contributor
Posts: 187
Registered: ‎05-22-2008
Message 1 of 6 (3,275 Views)
Accepted Solution

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.
Trusted Contributor
Posts: 90
Registered: ‎10-31-2002
Message 2 of 6 (3,275 Views)

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
Posts: 190
Registered: ‎02-09-2009
Message 3 of 6 (3,275 Views)

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
Posts: 25,685
Registered: ‎03-06-2006
Message 4 of 6 (3,275 Views)

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
Posts: 187
Registered: ‎05-22-2008
Message 5 of 6 (3,275 Views)

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.
Respected Contributor
Posts: 187
Registered: ‎05-22-2008
Message 6 of 6 (3,275 Views)

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

Closing the thread
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.