How to Assign an shell array to awk array?

Hello All,
Can you please help me with the below.

#!/bin/bash

ARR[0]="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1"
ARR[1]="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1"
ARR[2]="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1"

#printf "%s\n""${ARR[@]}" | /usr/bin/mutt -s "Hello Testing" abc@xyz.com

i=0
awk -v A="${ARR[@]}" '
BEGIN {
for (k in A)
print k } ' | /usr/bin/mutt -s "Hello Testing" abc@xyz.com

Output:

awk: fatal: `A' is not a legal variable name

try something like:

awk -v A="${ARR[0]}" '
BEGIN {
c=split(A,a);
for (i=1; i<=c; i++) print a } '

How about using split to populate an array once the variable is in awk like this:

awk -v A="$(printf "%s\n" "${ARR[@]}")" '
BEGIN {
split(A,B,"\n")
for (k in B)
print B[k] } '

Though it's printing all lines but everything is coming as a single line in email, instead of 3 different lines.

No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1 No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1 No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1

---------- Post updated at 04:33 PM ---------- Previous update was at 04:32 PM ----------

Hello,
I am getting the following output in email. appreciate your help.

No
Differences
In
Stage
Between
HASH_TOTALS
&
HASH_TOTALS_COMP
For
UNINUM:0722075
PROVIDER:5
EXTRACT_DT:30-SEP-12
VER_NUM:1

What does it look like in terminal, rather than email?

You man need ctrl-M on end of lines

Try:

awk -v A="$(printf "%s\n" "${ARR[@]}")" '
BEGIN {
v=split(A,B,"\n");
while(i++<v) printf " %s%c\n",B,13 } '

This script output looks correct when i direct the output to teriminal.
But not in email everything coming as single line

awk -v A="$(printf "%s\n" "${ARR[@]}")" '
BEGIN {
split(A,B,"\n\n")
for (k in B)
print B[k] } '

---------- Post updated at 04:42 PM ---------- Previous update was at 04:39 PM ----------

I see the output coming in email as below, can we remove the additional blank lines in between.

No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1 

No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1 

No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1 

Yes, this is a query of Outlook I think text lines are joined up into a paragraph unless they start with 1 or more spaces try:

awk -v A="$(printf "%s\n" "${ARR[@]}")" '
BEGIN {
v=split(A,B,"\n");
while(i++<v) print " " B } '

I am getting same output, everything in single line.

Outlook is infamous for imposing formatting, to the point <pre> tags or sending all text as email attachments isn't unheard of to make text line up/appear proper.

I do remember a workaround I used for this in the past, try two spaces at the front of each line.

awk -v A="$(printf "%s\n" "${ARR[@]}")" '
BEGIN {
v=split(A,B,"\n");
while(i++<v) print "  " B } '

It's also a good idea in outlook to select a nice non-proportional font for plain text messages.

1 Like

This looks good works as expected, also i am curious is the same output or workaround possible with just printf statement and with out using awk ? what should i do to achieve it?

Thanks much

how about printf " %s\n"

1 Like

In the your code while(i++<v) does v represents a number or a string?

Thank you.

It is a number as it is assigned the count of array elements produced by split .