Need to save output of echo and awk to a file


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Need to save output of echo and awk to a file
# 1  
Old 07-09-2019
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
Code:
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
Code:
$ 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?

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

Thanks.
# 2  
Old 07-09-2019
Quote:
Originally Posted by sand1234
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?

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

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:

Code:
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:

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

instead of the (basically equivalent but less readable)

Code:
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
# 3  
Old 07-09-2019
This works for me:
Code:
 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.
# 4  
Old 07-09-2019
Quote:
Originally Posted by bakunin
...
Code:
command1 &&  command2 || echo "something went wrong with command 1"

...
Actually it is
Code:
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.
Code:
{ 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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep echo awk print all output on one line

Hello, I've been trying to find the answer to this with Google and trying to browse the forums, but I haven't been able to come up with anything. If this has already been answered, please link me to the thread as I can't find it. I've been asked to write a script that pulls a list of our CPE... (51 Replies)
Discussion started by: rwalker
51 Replies

2. Shell Programming and Scripting

Save value from output of Corestat and save in a list for each core

I am trying to modify the "corestat v1.1" code which is in Perl.The typical output of this code is below: Core Utilization CoreId %Usr %Sys %Total ------ ----- ----- ------ 5 4.91 0.01 4.92 6 0.06 ... (0 Replies)
Discussion started by: Zam_1234
0 Replies

3. Homework & Coursework Questions

Save output into file bash scripting

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hi there. i have created a program that in the end it will give output like this 1 2 3 4 5 10 9 ... (1 Reply)
Discussion started by: shdin271
1 Replies

4. Shell Programming and Scripting

Echo awk output from its variable

Stumped with the formatting of the awk output when used with variables, e.g.: awk -F, 'BEGIN {OFS=","} print {$2,$3,$4}' $infile1 produces the desired output (with rows), but when echoing the variable below, the output is one continuous line var1=$(awk -F, 'BEGIN {OFS=","} print... (4 Replies)
Discussion started by: ux4me
4 Replies

5. Shell Programming and Scripting

Problem with writing to output - awk, echo

Hello all, I wrote this command line for some calculation on my given input files based on another input file which is a txt file. while read BAM REGION; do samtools view $BAM $REGION | awk '{if ($2==0) print $0}' | wc -l >>log.txt; echo "$REGION"; done >> log.txt <regions.txt It takes... (4 Replies)
Discussion started by: @man
4 Replies

6. Shell Programming and Scripting

Perl - save results to output file.

Can any one please help, the code works...I want the output of $result to be saved in an output.txt file which is lcoated in c:\\temp\\output.txt. $filepath="C:\\temp\\ip.txt"; open (HOSTLIST,"$filepath"); @hosts=(<HOSTLIST>); foreach $host(@hosts) { $results = `nslookup... (1 Reply)
Discussion started by: sureshcisco
1 Replies

7. Shell Programming and Scripting

Way to save output result of a program into another new file...

Does anybody know any alternative way to save output result of a program into another new file? I got try the command below: program_used input_file > new_output_file program_used input_file >> new_output_file Unfortunately, both the ">" and ">>" is not work at this case to save the output... (6 Replies)
Discussion started by: patrick87
6 Replies

8. Shell Programming and Scripting

Save cURL verbose output to file or do it like browser "save as.."

hi there ! i have exactly the same problem like this guy here https://www.unix.com/shell-programming-scripting/127668-getting-curl-output-verbose-file.html i am not able to save the curl verbose output.. the sollution in this thread (redirecting stderr to a file) does not work for me.... (0 Replies)
Discussion started by: crabmeat
0 Replies

9. Shell Programming and Scripting

let curl output to stdout AND save to a file

hello hackers. i have a curl process running as cgi directly pushing stdout to the client. but i want to additionally save that stream to a file at the same time. any directions madly welcome. thanks in advance (3 Replies)
Discussion started by: scarfake
3 Replies

10. Solaris

terminal output - save to file?

I have a window open on my ultra 10 - a terminal window connecting to a server. Is there any way I can log all output to this window to a log file on my ultra 10 ? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question

Featured Tech Videos