Accessing awk array from shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Accessing awk array from shell
# 1  
Old 08-23-2008
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[0]}) it doesn't work.

I read "all "the awk threads in this forum and came to two solutions, which both don't work in the desired way. Is there any other way to pass an array from awk back to the shell script? Or what's wrong with my solutions so far?

Any help is appreciated. Thanks in advance, Daniel

First try:
Code:
#!/bin/sh

declare -a source_path
declare -a target_path

source_path=`awk -F';' '{print $1}' backup_path`
target_path=`awk -F';' '{print $2}' backup_path`

# getting the right values
/bin/echo "Sources"
for a in ${source_path[@]} 
do
/bin/echo $a
done

/bin/echo "Target"
for a in ${target_path[@]} 
do
/bin/echo $a
done

#getting the wrong values  concatenated
/bin/echo ${source_path[0]} 
/bin/echo ${target_path[0]}

Also wrong:
Code:
awk -F';' '{ source_path[NR] = $1 } END { print source_path[1]; }' backup_path

/bin/echo ${source_path[0]}

File, to parse:
Code:
/Users/daniel/Documents/Test;/Volumes/Backup/auto/Test;
/Users/daniel/Documents/Test3;/Volumes/Backup/auto/Test3;
/Users/daniel/Documents/Test2;/Volumes/Backup/auto/Test2;

# 2  
Old 08-23-2008
Not necessary to use an array, try this:

Code:
#!/bin/sh

source_path=`awk -F';' '{print $1}' backup_path`
target_path=`awk -F';' '{print $2}' backup_path`

# getting the right values
/bin/echo "Sources"
for a in "$source_path"
do
  /bin/echo "$a"
done

/bin/echo "Target"
for a in "$target_path"
do
  /bin/echo "$a"
done

Regards

Last edited by Franklin52; 08-23-2008 at 09:10 AM..
# 3  
Old 08-24-2008
But this gives me only the whole list, doesn't it?
But what can i do, if i only want to access one single entry at one time?
Eg. in the beginning of the script i want to access the second entry, and later in the script the third.
Your solution , as well as my tries, only give me the complete list in this form:

Code:
/Users/daniel/Documents/Test
/Users/daniel/Documents/Test3
/Users/daniel/Documents/Test2

What i need is to be able to access each single entry.
Is there any way to do that in asimple way or do i need to split the output of awk again later on?
# 4  
Old 08-24-2008
You can access the entry within the loops, something like:

Code:
for a in "$source_path"
do
  cp "$a" /path/to/yourfiles
done

If you want to place them in an array for further use in your script:

Code:
i=0
for a in "$source_path"
do
  arr[$i]="$a"
  let i=i+1
done

To loop through an array:

Code:
len=${#arr[*]}
i=0
while [ $i -lt $len ]
do
  echo "${arr[$i]}"    # do something with element $i
  let i=i+1
done

Hope this helps.

Regards
# 5  
Old 08-24-2008
Thanks.
So the actual output of "source_path=`awk -F';' '{print $1}' backup_path`" is just a single variable, which later on has to be placed into an array?
Just for interest: Is there no other way to directly write the awk output into an array?

Thanks again,
Daniel
# 6  
Old 08-24-2008
Quote:
Originally Posted by bateman23
Thanks.
So the actual output of "source_path=`awk -F';' '{print $1}' backup_path`" is just a single variable, which later on has to be placed into an array?
Just for interest: Is there no other way to directly write the awk output into an array?

Thanks again,
Daniel
Sure, something like:

Code:
i=0

awk -F';' '{print $1}' backup_path | while read s
do
  arr[$i]="$s"
  let i=i+1
done

Regards
# 7  
Old 08-24-2008
Quote:
Originally Posted by bateman23
Thanks.
So the actual output of "source_path=`awk -F';' '{print $1}' backup_path`" is just a single variable, which later on has to be placed into an array?
Just for interest: Is there no other way to directly write the awk output into an array?

Code:
IFS=$'\n' ## not necessary if the elements have no whitespace
arr=( $( awk -F';' '{print $1}' backup_path) )

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Assign an shell array to awk array?

Hello All, Can you please help me with the below. #!/bin/bash ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1" ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5... (14 Replies)
Discussion started by: Ariean
14 Replies

2. Shell Programming and Scripting

Problem accessing array elements

Hi all, I can’t resolve an array element access issue on (Linux/pdksh) .. So I’m positing for advice.By the way - a friend demonstrated to me - same script works as expected under Solaris. I have been working on a documentation project where many *.jpg screen shots are used in the... (4 Replies)
Discussion started by: njdpo
4 Replies

3. Shell Programming and Scripting

accessing a variable or array of one script in another

hi all, i've a requirement like this. i want to access the contents of an array declared in one script,which is a bash script, to a second script which is a perl script. actually i want a sort of global variable which can be accessed in other script like environmen variables and also i can... (3 Replies)
Discussion started by: tprayush
3 Replies

4. Shell Programming and Scripting

Accessing variable from awk program in shell

Hi, I want to access a variable outside the awk program. My program is as below:- I can not access the exact value of k (See the last line of the program). #!/usr/bin/sh j=10 k=1 #k is declared outside awk awk ' BEGIN { i=1; j1="'"$j"'" printf("\n ## Value of j1 is %d ##", j1); ... (2 Replies)
Discussion started by: shouvik.mitra
2 Replies

5. Shell Programming and Scripting

Accessing array elements

Hi, My doubt is how to access array elements.. Situation is as below: #!/bin/ksh set -x typeset -i x=0 typeset -i y=0 typeset -i BID=0 typeset -i count=0 while ] ; do x=`expr $x + 1`; hwmgr show scsi > scsi.tmp while read line; do set... (1 Reply)
Discussion started by: mansa
1 Replies

6. Shell Programming and Scripting

Accessing shell arrays in awk

I am trying to pass a shell array I created to match on values amongst other things. I have a sample: #! /bin/ksh i=0 for id in `cat id.lst` do IDs=$id (( i=i+1 )) done cat sqloutput.txt | awk -F, -v ids=$ID '{ # Do stuff }' But it looks as if I cannot do this.... (1 Reply)
Discussion started by: Zombywoof
1 Replies

7. Shell Programming and Scripting

Accessing single elements of a awk array in END

How do I access one of the indices in array tst with the code below? tst=sprintf("%5.2f",Car / 12) When I scan thru the array with for ( i in tst ) { print i,tst } I get the output of: vec-7 144 But when I try this in the END print tst It looks like it's not set. What am... (6 Replies)
Discussion started by: timj123
6 Replies

8. Shell Programming and Scripting

Could someone give me an example of awk accessing array defined in Korn Shell?

As per title and much apprecieated! (2 Replies)
Discussion started by: biglau
2 Replies

9. Shell Programming and Scripting

Accessing Shell Variables in awk or sed

Hello, I wonder if it is possible to pass and use variables from shell environment into sed or awk. I am trying to achieve something similar to the following using sed or awk: var=some_regular_expression grep "$var" filename # Will extract lines from filename The following code,... (3 Replies)
Discussion started by: nasersh
3 Replies

10. Shell Programming and Scripting

accessing my first element of array

Hello everyonel, I have an array set like so num=4 read name arr=name I go through while loop to assign different values to different array element from 1 to 4. when I try to access the FIRST element of the array I get the last one first. Like if I say ${arr} it will show the last element... (4 Replies)
Discussion started by: afadaghi
4 Replies
Login or Register to Ask a Question