Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Need to save output of echo and awk to a file Post 303036724 by bakunin on Tuesday 9th of July 2019 10:14:39 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 12:18 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy