When I run the following hasys command with awk , I get output with machines names as the following:

hasys -display|grep Shutdown | awk '{print $1}'

please advice how to print the machines names in one line as the following: example

hasys -display|grep Shutdown | awk '{print $1}'

    machine1a machine1b

what I need to add in my awk syntax so I will able to print the names in one line?


You can say:

hasys -display | grep Shutdown | awk '{print $1}' ORS=' '

The grep with awk is redundant:

 hasys -display | awk '/Shutdown/ { printf "%s ", $1 }'
  • 2
    If he wants a newline after the last record, he could do: awk '/Shutdown/ {printf "%s ", $1} END {print ""}' – Johnny Feb 18 '14 at 3:05
hasys -display | awk '/Shutdown/ {print $1}' | paste -sd ' ' -

Contrary to @devnull's and @jasonwryan's, this one doesn't add a trailing space and terminates the line. paste -sd<sep> - is the generic idiom to join lines into a <sep> separated list.

 echo $( hasys .. the whole thing )

Just for fun

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy