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 Bondherawk '{print $3, $4}', command 2, outputs:
AGE 21
AGE 420
AGE 2345678909876543234567890876543234567890876543When 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 2345678909876543234567890876543234567890876543Because 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 2345678909876543234567890876543234567890876543Previous attempts at appending "\n" have resulted in:
John Bender
Bohn Jender
Jen Bondher
AGE 21
AGE 420
AGE 2345678909876543234567890876543234567890876543..which is certainly unwanted.
2 Answers
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.txtOriginal 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
spam11
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
ARGINDvariable.NRis total record processed so far, andFNRis 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 2345678909876543234567890876543234567890876543Use 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 2345678909876543234567890876543234567890876543Variation 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 3