Need to save output of echo and awk to a file

Hi,

I am generating a YAML file from a hosts file, but am having trouble saving it to a new file.

hosts file

127.0.0.1      localhost
192.168.1.2    host1
192.168.1.3    host2
192.168.1.4    host3
192.168.1.5    host4

YAML file

$ echo 'host_entries:' && awk '{printf "  %s:\n     ip: %s\n", $2, $1}' hosts2.txt
host_entries:
  localhost:
     ip: 127.0.0.1
  host1:
     ip: 192.168.1.2
  host2:
     ip: 192.168.1.3
  host3:
     ip: 192.168.1.4
  host4:
     ip: 192.168.1.5

When I try and save the file to hosts2.yml, it looks like echo is printing to STDOUT. Is there anyway to pass the result of echo to awk, or maybe a better way to do this?

$ echo 'host_entries:' && awk '{printf "  %s:\n     ip: %s\n", $2, $1}' hosts2.txt > hosts2.yml
host_entries:

Thanks.

You can make awk print the text instead of using the echo -keyword. The BEGIN-clause in awk is for that: whatever is in the BEGIN clause is executed before any text from the input file is processed:

awk 'BEGIN {
             printf "host entries:\n";
          }
          {
             printf "  %s:\n     ip: %s\n", $2, $1;
          }' hosts2.txt

This should do the same as the construct with echo . Furthermore, a little hint: if you connect two commands (like the awk and the echo ) with the && operator you effectively say: execute the second command only if the first command exits without error.

This can make sense if the second command really depends on the successful execution of the first. But even in this case it is clearer to write it in long form:

if command1 ; then
     command2
else
     echo "something went wrong with command 1"
fi

instead of the (basically equivalent but less readable)

command1 &&  command2 || echo "something went wrong with command 1"

In case of awk and echo it doesn't make any sense because, honestly, how is echo supposed to not succeed?

I hope this helps.

bakunin

This works for me:

 a=$(echo 'hi there:' && awk '{printf "  %s:\n     ip: %s\n", $2, $1 }' host2.txt)
 echo "$a" > yaml_file

Note I added 'hi there', should be something else.
If you run a series of commands as a child process and save the all the outputs to a variable, stdout gets written to that variable.
There are other ways to do this, as a matter of taste or readability of code.

Actually it is

command1 &&  command2 || echo "something went wrong with command 1 or command 2"

Test it with true and false commands!

Another solution to the original question: redirect a { command1; command2; } group.

{ echo 'host_entries:' && awk '{printf "  %s:\n     ip: %s\n", $2, $1}' hosts2.txt; } > hosts2.yml

As was said earlier, an unconditional ; is simpler than the && .
The command group is smarter than a ( command1; command2 ) sub shell.
Note that the command group requires a ; or a newline before the closing } - while the sub shell does not require it before the closing ) .
BTW, in the previous post the $( ) is a sub shell, too. But all the output is stored in a variable aka memory first - this is far from being efficient.