Bash: How do I combine two arrays into a third array? [duplicate] - arrays

This question already has answers here:
Merging two Bash arrays into Cartesian key:value pairs
(5 answers)
Closed 6 years ago.
I have two arrays: array1 and array2:
array1=( a b c )
array2=( 1 2 3 )
How do I make a third array, array3:
array3=( a b c 1 2 3 )
This question is different from Combine arrays in the beginning of a for-loop (Bash) because this deals exclusively with combining arrays, not whether such a statement is legal within a for loop. Thus, it is more general.

From The Advanced Bash Scripting Guide example 27-10, with a correction:
declare -a array1=( zero1 one1 two1 )
declare -a array2=( [0]=zero2 [2]=two2 [3]=three2 )
dest=( "${array1[#]}" "${array2[#]}" )
Thus, for my case, it's:
array3=( "${array1[#]}" "${array2[#]}" )

Related

Converting bash array into string with - in between [duplicate]

This question already has answers here:
How can I join elements of an array in Bash?
(34 answers)
Closed 4 months ago.
How do I convert an array into a string with a dash '-' in between in bash. For eg, I have this array
arr=(a b c d e)
I want to convert it into a string "a-b-c-d".
I figured out this "a-b-c-d-e," but there is an unwanted dash at the end. Is there an efficient way of doing this?
Thanks
This is where the "${arr[*]}" expansion form is useful (note the double quotes and the * index). This joins the array elements using the first character of the IFS variable
arr=(a b c d e)
joined=$(IFS='-'; echo "${arr[*]}")
declare -p joined
# => declare -- joined="a-b-c-d-e"
But if you want all-but-the-last elements, you'll combine this with the ${var:offset:length} expansion
joined=$(IFS='-'; echo "${arr[*]:0: ${#arr[#]} - 1}")
declare -p joined
# => declare -- joined="a-b-c-d"
This one's a little tricker. The offset and length parts of that expansion are arithmetic expressions. I'm calculating the length as "the number of elements in the array minus one".
Note how I'm defining IFS inside the command substitution parentheses: this is overriding the variable in the subshell of the command substitution, so it won't affect the IFS variable in your current shell.
Using awk if you want to remove the last entry
$ awk -vOFS=- '{NF--;$1=$1}1' <<<${arr[#]}
a-b-c-d
or the complete array
$ awk -vOFS=- '{$1=$1}1' <<<${arr[#]}
a-b-c-d-e

bash index multiple different arrays at same index for loop [duplicate]

This question already has answers here:
bash script loop through two variables in lock step
(2 answers)
bash shell script two variables in for loop
(8 answers)
bash script loop multiple variables
(2 answers)
Closed 1 year ago.
Let's say I have three arrays that look like the following:
a=(banana apple grape)
b=(1 2 3)
c=(yellow red purple)
If I wanted to loop through such that each index of each variable is matched, how would I do this? For example, I would want something like the following:
for i in range(0,len(a))
do
echo "$a[i] $b[i] $c[i]"
done
such that the results would be:
banana 1 yellow
apple 2 red
grape 3 purple
The syntax to access array elements is ${array[index]}.
for ((i=0 ; i < ${#a[#]} ; ++i)) ; do
echo "${a[i]} ${b[i]} ${c[i]}"
done
You can use seq to simulate Python's range:
for i in $(seq 1 ${#a[#]}) ; do
echo "${a[i-1]} ${b[i-1]} ${c[i-1]}"
done
where ${#array[#]} returns the size of the array.

Create an array in bash from given start and end point [duplicate]

This question already has answers here:
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 4 years ago.
I have this code that works:
$ array=( {100..300..100} )
$ for k in ${array[#]} ; do echo $k ; done
100
200
300
I want to parametrize the start and end points (and also the increment, because why not?)
I tried this:
$ low=100
$ high=300
$ incr=100
$ array=( {$low..$high..$incr} )
But it didn't work:
$ for k in ${array[#]} ; do echo $k ; done
{100..300..100}
What am I doing wrong?
From bash manual:
A sequence expression takes the form {x..y[..incr]}, where x and y are either integers or single characters, and incr, an optional increment, is an integer.
So, parameter and variable expansion are not performed on x, y and incr. You should use seq:
arr=( $(seq $low $incr $high) )

How can I sort array in bash? [duplicate]

This question already has answers here:
How to sort an array in Bash
(20 answers)
Closed 4 years ago.
for example, if I want to write a script that get strings as arguments, and I want to insert them to array array_of_args and then I want to sort this array and to make sorting array.
How can I do it?
I thought to sort the array (and print it to stdout) in the next way:
array_of_args=("$#")
# sort_array=()
# i=0
for string in "${array_of_args[#]}"; do
echo "${string}"
done | sort
But I don't know how to insert the sorting values to array ( to sort_array)..
You can use following script to sort input argument that may contain whitespace, newlines, glob characters or any other special characters:
#!/usr/bin/env bash
local args=("$#") # create an array of arguments
local sarr=() # initialise an array for sorted arguments
# use printf '%s\0' "${args[#]}" | sort -z to print each argument delimited
# by NUL character and sort it
# iterate through sorted arguments and add it in sorted array
if (( $# )); then
while IFS= read -rd '' el; do
sarr+=("$el")
done < <(printf '%s\0' "${args[#]}" | sort -z)
fi
# examine sorted array
declare -p sarr

awk - print last field $NF of array [duplicate]

This question already has answers here:
How can I get the length of an array in awk?
(9 answers)
Closed 6 years ago.
is any chance how to print - lets say example:
A/b/c/d/e/f A B C D E F G|H|I|J|
I would like to split fields to array and print:
awk -v OFS="\t" '{split($1,a,"/"); split($3,b,"|"); print a[LAST_FIELD??],$1,$2,$3,b[1],b[2]}' input
the result should be:
f A B C g H I
I am not sure how to define "LAST_FIELD" in my array. Thank you for any help.
split returns the number of elements, so you can use that:
n = split($1,a,"/")
then:
print a[n]
You can use this awk:
awk -v OFS="\t" '{lena=split($1,a,"/"); split($NF,b,"|");
print a[lena],$2,$3,$4,b[1],b[2],b[3]}' file
f A B C G H I

Resources