Place newline after every command
Solution 1:
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
Solution 2:
Just because it's possible to do so, here's what alternative approaches can be taken.
ARGIND with NR and FNR variables
This approach runs through file twice, and at the moment of running through file second time, it prints a newline, which is achieved via use of a few interesting ideas.
For processing same file but extracting different info, file is given on command line twice.
How do we distinguish when we're running through first file ( to extract fields $1 and $2 ) and when through second ? Via using
ARGIND
variable.NR
is total record processed so far, andFNR
is total record processed in current file. With the first file, they are the same.When will they differ ? When we get to second file. At this moment we need to print newline, but somehow prevent it being printed for other values whereNR > FNR
. We raise a flag ( via flag variable ).
Sample output:
$ awk '!flag && NR>FNR {print "";flag=1} ARGIND==1{print $1,$2}ARGIND==2{print $3,$4}' data.txt data.txt
John Bender
Bohn Jender
Jen Bondher
AGE 21
AGE 420
AGE 2345678909876543234567890876543234567890876543
Use of END clause
As mentioned in comments under heemayls answer, when using two separate awk commands, one can use END
block to print something once processing of file is complete
$ awk '{print $1,$2}END{print "\n"}' data.txt; awk '{print $3,$4}' data.txt
John Bender
Bohn Jender
Jen Bondher
AGE 21
AGE 420
AGE 2345678909876543234567890876543234567890876543
Variation on END block with associative arrays
Since your $3 block is basically same thing over and over ( string "AGE" ) you don't have to store it, but you can store $4 into associative array. Then in END block you can iterate over stored values after printing newline.
$ awk '{print $1,$2;age[i++]=$4}END{print"";for(var in age) print "AGE",age[var]}' data.txt
John Bender
Bohn Jender
Jen Bondher
AGE 21
AGE 420
AGE 2345678909876543234567890876543234567890876543