How to Assign an shell array to awk array?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Assign an shell array to awk array?
# 1  
Old 09-19-2013
How to Assign an shell array to awk array?

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

Code:
#!/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[i]="${ARR[@]}" '
BEGIN {
for (k in A)
print k } ' | /usr/bin/mutt -s "Hello Testing" abc@xyz.com

Output:
Code:
awk: fatal: `A[i]' is not a legal variable name

# 2  
Old 09-19-2013
try something like:
Code:
awk -v A="${ARR[0]}" '
BEGIN {
c=split(A,a);
for (i=1; i<=c; i++) print a[i] } '

# 3  
Old 09-19-2013
How about using split to populate an array once the variable is in awk like this:

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

# 4  
Old 09-19-2013
Quote:
Originally Posted by Chubler_XL
How about using split to populate an array once the variable is in awk like this:

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

Code:
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 ----------

Quote:
Originally Posted by rdrtx1
try something like:
Code:
awk -v A="${ARR[0]}" '
BEGIN {
c=split(A,a);
for (i=1; i<=c; i++) print a[i] } '

Hello,
I am getting the following output in email. appreciate your help.
Code:
No
Differences
In
Stage
Between
HASH_TOTALS
&
HASH_TOTALS_COMP
For
UNINUM:0722075
PROVIDER:5
EXTRACT_DT:30-SEP-12
VER_NUM:1

# 5  
Old 09-19-2013
What does it look like in terminal, rather than email?
# 6  
Old 09-19-2013
You man need ctrl-M on end of lines

Try:

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

# 7  
Old 09-19-2013
Quote:
Originally Posted by Corona688
What does it look like in terminal, rather than email?
This script output looks correct when i direct the output to teriminal.
But not in email everything coming as single line

Code:
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 ----------

Quote:
Originally Posted by Chubler_XL
You man need ctrl-M on end of lines

Try:

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

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

Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assign Two Dimensional Array In Bash At Once

Hi, I have a 10*10 two dimensional array. How do I assign value to all it's 100 elements at once? I don't want to open two for loops and assign one by one. Thanks, Shuri (1 Reply)
Discussion started by: shurimano
1 Replies

2. Shell Programming and Scripting

awk assign output of array to specific field-number

With this script i want to print the output to a specific field-number . Can anybody help? awk 'NR=FNR{split(FILENAME,fn,"_");nr=$2;f = $1} END{for (i=1;i<=f;i++) print i,$fn=nr}' input_5.csv input_6.csvinput_5.csv 4 135 5 185 6 85 11 30input_6.csv 1 90 3 58 4 135 7 60 8 55 10... (1 Reply)
Discussion started by: sdf
1 Replies

3. Shell Programming and Scripting

Assign zero to strings that don't appear in block, store result in AWK array

Hi to all, I have this input: <group> <x "2">Group D</x> <x "3">Group B</x> <x "1">Group A</x> </group> <group> <x "1">Group E</x> <x "0">Group B</x> <x "1">Group C</x> </group> <group> ... (11 Replies)
Discussion started by: Ophiuchus
11 Replies

4. Shell Programming and Scripting

Assign value to array separated by #

Hi all , I have a string like para1#para2#para3 i want to assign para1 as first element para2 as second and so on i tried IFS=# set -A array para1#para2#para3 echo ${array} para1 para2 para3 i want echo ${array} para1 (2 Replies)
Discussion started by: max_hammer
2 Replies

5. Shell Programming and Scripting

assign value to array variable

Hi, I have a piece of code as follows: i=0 while read LINE do var = "$LINE" i=$((i+1)) echo "${var}" done < file I want to assign value to the array var. However, when i execute the script i get a error. Please can you help me know what i am missing. I ultimately want to... (2 Replies)
Discussion started by: sunrexstar
2 Replies

6. Shell Programming and Scripting

Cut text and assign them into array

file.txt : is delimiter: abc:def:ghi jkl:mno: pqr 123:456:789 if I do the cut command, and cut the first column, and echo it out I will get the output: abc jkl 123 How can I assign the column of text that I've cut into Array? e.g If I were to echo array array it will output as:... (9 Replies)
Discussion started by: andylbh
9 Replies

7. Shell Programming and Scripting

How to assign a variable to an array

I want to ask the user to enter an X amount of file names. I want to put those names into an array and then loop back through them to verify they are in the directory. 1st- How would I assign the value to an array and what is the correct syntax. 2nd- how would i reference that array after I... (3 Replies)
Discussion started by: tvb2727
3 Replies

8. Shell Programming and Scripting

retaining awk array in shell

Is there a way to retain the awk array in the shell. My requirement file a.txt A B Count 10 1 25 10 2 20 10 3 21 11 1 20 11 2 22 12 2 40 12 3 15 A and B are my variables and count... (6 Replies)
Discussion started by: jagpreetc
6 Replies

9. Shell Programming and Scripting

Accessing awk array from shell

Hi, i want awk to read a file and place it's content into two arrays. When trying to read these arrays with a "for a in ${source_path} "-Loop it gives the right result. But when trying to access directly (/bin/echo ${source_path}) it doesn't work. I read "all "the awk threads in this forum and... (6 Replies)
Discussion started by: bateman23
6 Replies

10. Shell Programming and Scripting

assign awk array with printf

I am trying to assign a awk array for further processing later in the script. I can't seem to figure it out. If someone could look at this and help me, I would very much appreciate it. Thanks in Advance. for ( x = 1 ; x <= Var ; x++ ) { if ( x in varr ) { ... (2 Replies)
Discussion started by: timj123
2 Replies
Login or Register to Ask a Question