Ubuntu – Place newline after every command

awkbashscripts

How do I implement a carriage or newline return in every awk command in a shell script, such that each command's output is separated from the next?

Perhaps I need more specification. I have two awk commands, listed below:

awk {print $1, $2}, awk command 1, outputs:

John Bender
Bohn Jender
Jen Bondher

awk '{print $3, $4}', command 2, outputs:

AGE 21
AGE 420
AGE 2345678909876543234567890876543234567890876543

When combined in a shell script, and run with ./<testscript>.sh <textfile>, the command line obviously outputs:

John Bender
Bohn Jender
Jen Bondher
AGE 21
AGE 420
AGE 2345678909876543234567890876543234567890876543

Because I want the newline append PER awk command, I want the output to look simply like:

John Bender
Bohn Jender
Jen Bondher

AGE 21
AGE 420
AGE 2345678909876543234567890876543234567890876543

Previous attempts at appending "\n" have resulted in:

John Bender

Bohn Jender

Jen Bondher

AGE 21

AGE 420

AGE 2345678909876543234567890876543234567890876543

..which is certainly unwanted.

Best Answer

As you are running from inside a shell script, just add echo after each awk command i.e. in between the commands you want to get separate outputs. echo adds a newline. For example:

awk '{print $1 $2}' file.txt
echo
awk '{print $3, $4}' file.txt

Original answer:

Append printf "\n" at the end of each awk action {}.

printf "\n" will print a newline.

Example:

% awk '{print $1; printf "\n"}' <<<$'foo bar\nspam egg'
foo

spam