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:
#!/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:
awk -F';' '{ source_path[NR] = $1 } END { print source_path[1]; }' backup_path
/bin/echo ${source_path[0]}
File, to parse:
/Users/daniel/Documents/Test;/Volumes/Backup/auto/Test;
/Users/daniel/Documents/Test3;/Volumes/Backup/auto/Test3;
/Users/daniel/Documents/Test2;/Volumes/Backup/auto/Test2;
Not necessary to use an array, try this:
#!/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
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:
/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?
You can access the entry within the loops, something like:
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:
i=0
for a in "$source_path"
do
arr[$i]="$a"
let i=i+1
done
To loop through an array:
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
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:
i=0
awk -F';' '{print $1}' backup_path | while read s
do
arr[$i]="$s"
let i=i+1
done
Regards
IFS=$'\n' ## not necessary if the elements have no whitespace
arr=( $( awk -F';' '{print $1}' backup_path) )