Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
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
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.
Highlighted
Bijeesh
Respected Contributor

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

Closing the thread